Find numbers n in base 2 where n is prime and all it's digits are 1
Find numbers n in base 2 where n is prime and all it's digits are 1 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Find numbers n in base 2 where n is prime and all it's digits are 1
Let 1 < n < 10000
Factor
Numbers in base 2 whose digits are all 1: also known as the powers of 2 minus 1. <lang factor>USING: lists lists.lazy math math.primes prettyprint ;
2 lfrom [ 2^ 1 - ] <lazy-map> [ prime? ] lfilter [ 10,000 < ] lwhile list>array .</lang>
- Output:
{ 3 7 31 127 8191 }
Ring
<lang ring> load "stdlib.ring"
num = 0 limit = 6 decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
see "working..." + nl see "the first 5 elements of sequence are:" + nl
for n = 1 to 10000
if isprime(n) flag = 1 bn = decimaltobase(n,2) for m = 1 to len(bn) if not(bn[m] = "1") flag = 0 exit ok next if flag = 1 num = num + 1 if num < limit see "" + num + ". " + "number = " + n + " in base 2 = " + bn + nl ok ok ok
next
see "done..." + nl
func decimaltobase(nr,base)
binList = [] binary = 0 remainder = 1 while(nr != 0) remainder = nr % base ind = find(decList,remainder) rem = baseList[ind] add(binList,rem) nr = floor(nr/base) end binlist = reverse(binList) binList = list2str(binList) binList = substr(binList,nl,"") return binList
</lang>
- Output:
working... the first 5 elements of sequence are: 1. number = 3 in base 2 = 11 2. number = 7 in base 2 = 111 3. number = 31 in base 2 = 11111 4. number = 127 in base 2 = 1111111 5. number = 8191 in base 2 = 1111111111111 done...