Palindromic primes in base 16: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 16: Line 16:


for n = 1 to limit
for n = 1 to limit
hex = decimaltobase(n,16)
hex = hex(n)
if ispalindrome(hex) and isprime(n)
if ispalindrome(hex) and isprime(n)
see "" + hex + " "
see "" + upper(hex) + " "
row = row + 1
row = row + 1
if row%5 = 0
if row%5 = 0
Line 28: Line 28:
see nl + "Found " + row + " palindromic primes in base 16" + nl
see nl + "Found " + row + " palindromic primes in base 16" + nl
see "done..." + nl
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>
</lang>
{{out}}
{{out}}

Revision as of 13:44, 23 June 2021

Palindromic primes in base 16 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 palindromic primes in base 16, where n < 500



Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Palindromic primes in base 16:" + nl row = 0 decList = 0:15 baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"] limit = 500

for n = 1 to limit

   hex = hex(n)
   if ispalindrome(hex) and isprime(n)
      see "" + upper(hex) + " "
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " palindromic primes in base 16" + nl see "done..." + nl </lang>

Output:
working...
Palindromic primes in base 16:
2 3 5 7 B 
D 11 101 151 161 
191 1B1 1C1 
Found 13 palindromic primes in base 16
done...