Find numbers n in base 2 where n is prime and all it's digits are 1: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Blanked the page)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Draft task}}

;Task:
Find numbers n in base 2 where n is prime and all it's digits are 1
<br>Let 1 < n < 10000

=={{header|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>
{{out}}
<pre>
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...

</pre>

Latest revision as of 02:07, 9 March 2021