little-endian radix conversion and unconversion in Pari/GP:
tobase(n,x)= my(l=List); while(x>0, my(r=x%n); listput(l,r); x-=r; x/=n); l;
tonumber(b,L)= my(x=0); my(n=1); foreach(L, d, x+=d*n; n*=b); x;
fixed-width least-significant digits (pads with zeros if necessary to achieve width):
towidthbase(width,n,x)= my(l=List); for(i=1, width, my(r=x%n); listput(l,r); x-=r; x/=n); l;
we demonstrate using these functions to search for primes containing only certain digits in base 10. we first generate numbers in base 2 then interpret the bit string as base 10.
c=0; for(i=0, 2^10, x=tonumber(10, tobase(2,i)); if(isprime(x), print(x); c+=1)); print("count="c)
11 101 10111 101111 1011001 1100101 10010101 10011101 10100011 10101101 10110011 10111001 11000111 11100101 11110111 11111101 100100111 100111001 101001001 101001011 101100011 101101111 101111011 101111111 110010101 110101001 110111011 111000101 111001001 111010111 1000001011 1000010101 1000011011 1000110101 1001000111 1001001011 1001010011 1001110111 1010000011 1010000111 1010001101 1010010011 1010011111 1010100011 1010110001 1010111111 1011000101 1011110011 1100001101 1100010001 1100101111 1101001001 1101010111 1101110011 1110011101 1110110011 1111011101 1111100101 1111110001 (count=59 up to 10 digits)
note: the next repunit prime after 11 in base 10 is (10^19 - 1)/9 so is beyond the range of all these lists (OEIS A004023).
c=0; for(w=1, 9, for(i=0, 2^w-1, l=towidthbase(w,2,i); for(j=1, w, l[j]+=1); x=tonumber(10,l); if(isprime(x), print(x); c+=1))); print("count="c)
2 11 211 2111 2221 12211 21121 21211 21221 22111 111121 111211 112111 112121 1111211 1121221 1212121 1212221 1221221 2121121 2211211 2221111 11221211 12111221 12121121 12121211 12122111 12122221 12212111 12222121 12222211 21112121 21121211 21122111 21122221 21212111 21222211 22111121 22112221 22221211 111112121 111121121 111221221 112111211 112112111 112212211 112221211 121111121 121112111 121211221 122111111 211122211 211212121 211222111 211222211 212111111 212122121 221112121 221212111 222221221 (count=60 up to 9 digits)
c=0; for(i=0, 3^6, x=tonumber(10, tobase(3,i)); if(isprime(x), print(x); c+=1)); print("count="c)
2 11 101 211 1021 1201 2011 2111 2221 10111 10211 12011 12101 12211 20011 20021 20101 20201 21001 21011 21101 21121 21211 21221 22111 101021 101111 101221 102001 102101 102121 110221 111121 111211 112111 112121 120011 120121 121001 121021 122011 122021 122201 200201 201011 201101 201121 201211 202001 202021 202121 202201 210011 210101 220021 221021 221101 221201 222011 (count=59 up to 6 digits)
previously: composites with low digits.
No comments :
Post a Comment