Robert James Liguori
,
Author
Feb 25, 2017 08:33:09
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
staff
Feb 25, 2017 08:47:09
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
Feb 25, 2017 08:55:26
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
Feb 25, 2017 09:28:14
This is what I came up with... improvements?
Jeanne Boyarsky
,
author & internet detective
staff
Feb 25, 2017 10:15:09
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
Feb 25, 2017 11:11:22
Thanks... I'll look at your code in a sec... I had an error in there, this fixes it:
Piet Souris
,
Bartender
staff
Feb 25, 2017 12:51:41
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
staff
Feb 25, 2017 13:01:30
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
Feb 25, 2017 13:50:04
Ha Ha!!!
I discovered my own prime with this code, the Liguori Prime!
http://robertjliguori.blogspot.com/2017/02/liguori-primes.html
Piet Souris
,
Bartender
staff
Feb 25, 2017 17:54:27
My test was incomplete. Here is the code I was referring to:
Robert James Liguori
,
Author
Feb 25, 2017 20:13:46
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
Feb 25, 2017 20:53:02
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
Feb 25, 2017 21:25:31
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
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com