Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cyclops Regex  RSS feed

 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head spinning on this one...

'need a regex to find cyclop strings...

e.g., 101, 22722, 9993999,  etc

I wish to use it for this: http://robertjliguori.blogspot.com/2017/02/palindromeprimeutils.html as inspired by this: https://www.youtube.com/watch?v=HPfAnX5blO0

Can anyone help with the expression?

Thanks,
Robert
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have to/want to use a regex? It's easy to do by converting to a String and then using a loop.
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Why do you have to/want to use a regex? It's easy to do by converting to a String and then using a loop.


Thanks, I'm working on it now... wish me luck.
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I came up with... improvements?

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three ideas for improvement:

1) There's no need to call new String because you already have a String from calling toString(). So you can simplify that one line to


2) StringBuilder would be better than StringBuffer. No need to have the old threadsafe class.

3)


Is the same as

Judgment call on which is easier to read.
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... I'll look at your code in a sec... I had an error in there, this fixes it:

 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach is to check for the length, must be uneven. Then make a frequency count of the characters. There should be two frequencies, of which one = 1. If so, check the middle character and the first character; they should be unequal.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or check the first/middle characters to check they are unequal. Then remove the middle char and check all others are the same.

Many ways .
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha Ha!!!

I discovered my own prime with this code, the Liguori Prime!

http://robertjliguori.blogspot.com/2017/02/liguori-primes.html
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My test was incomplete. Here is the code I was referring to:
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:My test was incomplete. Here is the code I was referring to:


Thank you so much!  I will do some speed test and will use the fastest method... I'll let you know if yours is the fastest.
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert James Liguori wrote:
Piet Souris wrote:My test was incomplete. Here is the code I was referring to:


While I do like your algorithm better... it looks like mine is a tad faster...

For 1,000,000,000 iterations...

Cyclop algorithm #1:
Three minutes, five seconds.
25/02/17 22:41:10 START
25/02/17 22:44:15 END

Cyclop algorithm #2:
Six minutes, 3 seconds
25/02/17 22:44:36 START
25/02/17 22:50:39 END

* I believe we were equally as fast for smaller numbers (i.e., for 100,000,000 I was just two seconds faster)  , but yours slowed up as the numbers got bigger.


Thanks again though!!!
Robert
 
Robert James Liguori
Author
Ranch Hand
Posts: 583
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the primes method I need to speed up... taking forever... and it's code i snagged from stackoverflow:

Anyone have a faster method to get square roots of BigIntegers?

Reference: https://github.com/robertjliguori/PalindronePrimeUtils/blob/master/src/main/java/com/stackoverflow/utils/BigIntSqRoot.java

Robert
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!