Join PrimeGrid
Returning Participants
Community
Leader Boards
Results
Other
drummers-lowrise
|
Message boards :
General discussion :
Get prime from concatenating first n numbers
Author |
Message |
|
Numberphile's newest video is The Most Wanted Prime Number, featuring N. J. A. Sloane from the OEIS.
They want to find a prime number by concatenating the (decimal) representations of 1, 2, 3, ..., n. You can search in PARI/GP like this:
b=10;N=1;for(n=2,+oo,N=N*b^(1+logint(n,b))+n;ispseudoprime(N)&&print(n))
If you change the first part to other bases, like b=12, you find examples.
/JeppeSN | |
|
|
I see that:
82 81 80 79 78 ... 11 10 9 8 7 6 5 4 3 2 1
is a prime number.
/JeppeSN | |
|
|
Numberphile's newest video is The Most Wanted Prime Number, featuring N. J. A. Sloane from the OEIS.
They want to find a prime number by concatenating the (decimal) representations of 1, 2, 3, ..., n. You can search in PARI/GP like this:
b=10;N=1;for(n=2,+oo,N=N*b^(1+logint(n,b))+n;ispseudoprime(N)&&print(n))
If you change the first part to other bases, like b=12, you find examples.
/JeppeSN
Nice !
____________
"Accidit in puncto, quod non contingit in anno."
Something that does not occur in a year may, perchance, happen in a moment. | |
|
|
Turns out 82 is followed by 37765; see A176024. Also see Mersenneforum thread Smarandache prime(s). /JeppeSN | |
|
Bur Volunteer tester
 Send message
Joined: 25 Feb 20 Posts: 511 ID: 1241833 Credit: 407,815,873 RAC: 10,359
                
|
Jeppe, I tried the same, but my code for creating the number is a bit more complicated... :)
m=2000;p=0;k=0;for (i=1,m,k+=ceil(log(i)/log(10));p+=i*10^k;if(ispseudoprime(p),print("Prime: ",p)))
And the other suggested format of increasing and then decreasing values like 12345678910987654321:
updown(r,s) = {
b=s;
for(a=r,s-1,
p=0;k=0;for (i=a,b,p+=i*10^k;k+=(floor(log(i)/log(10))+1));
q=0;k=0;for (i=1,b-a,j=b-i;q+=j*10^k;k+=(floor(log(j)/log(10))+1));
p+=q*10^(floor(log(p)/log(10))+1);
\\ print(p);
if(ispseudoprime(p),print("yay: ",p," is prime!"))
)
}
And to run it: for(i=2,100,updown(1,i)) tests all number with 2 ... 100 in the middle and beginning at 1 to the middle value. Such as 121, 232, 12321, 343, 23432, 1234321, ...
Your slim code could be used there as well though. I got to understand what's going on there at first though.
____________
1281979 * 2^485014 + 1 is prime ... no further hits up to: n = 5,700,000 | |
|
|
Where you say ceil(log(i)/log(10)), you can use 1+logint(i, 10) instead. Otherwise, it is more or less the same.
The good thing about logint (integer logarithm) is, it does not go through floating-point. It simply finds the "integer logarithm" of i, to base 10. You do not have to care about the value of the default realprecision.
Oh, and then I (ab)use the short-circuiting behavior of &&. So instead of:
if(b, something())
you can say:
b && something()
/JeppeSN | |
|
Bur Volunteer tester
 Send message
Joined: 25 Feb 20 Posts: 511 ID: 1241833 Credit: 407,815,873 RAC: 10,359
                
|
Ah, I had read ?logint but didn't see why it could be used at first.
So the output of logint(a,b) is always identical to floor(log_b(a)) it just saves computing time by not going through the actual calculation of the log?
Interesting take on the & instead of if. Now I realized I actually used that a couple of times before just not in such simple constructs but only within an if clause to indicate progress by throwing an &!(print(...)) in there. Btw, is it convention that print(a) returns 0 upon successfully printing?
____________
1281979 * 2^485014 + 1 is prime ... no further hits up to: n = 5,700,000 | |
|
|
Btw, is it convention that print(a) returns 0 upon successfully printing?
Yes, in PARI/GP, a "procedure" is a function which does not return anything (or returns the special void object, according to the user's manual). But this void thing will become a zero if you pick it up. For example:
print("hello")
-print("hello")
a=print("hello")
In the first example, the return value is void and is suppressed (not seen). In the second example, we force the use of void by negating it, and it becomes zero. This is like the 1&&print("hello") you ask for. In the third example above, again we get zero, this time stored in the variable a.
If in your home-made function, it reaches a return() with no argument, it behaves the same. It is allowed to omit the parenthesis.
/JeppeSN | |
|
Post to thread
Message boards :
General discussion :
Get prime from concatenating first n numbers |