Nice primes
Nice primes
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
- Take an integer n, greater than zero.
- sumn is sum of n's digits
- If sumn's lenght greater than 1 repeat the (2) for n = sumn
- Stop when sumn's length equal to 1
If n and sumn are prime then n is Nice prime
Let 500 < n < 1000
- Example
853 (prime) 8 + 5 + 3 = 16 1 + 6 = 7 (prime)
Ring
<lang ring> load "stdlib.ring"
num = 0 limit1 = 500 limit2 = 1000
see "working..." + nl see "Nice numbers are:" + nl
for n = limit1 to limit2
strn = string(n) while true sumn = 0 for m = 1 to len(strn) sumn = sumn + number(strn[m]) next if len(string(sumn)) = 1 exit ok strn = string(sumn) end if isprime(n) and isprime(sumn) num = num + 1 see "" + num + ": " + n + " sumn = " + sumn + nl ok
next
see "done..." + nl </lang>
- Output:
working... Nice numbers are: 1: 509 sumn = 5 2: 547 sumn = 7 3: 563 sumn = 5 4: 569 sumn = 2 5: 587 sumn = 2 6: 599 sumn = 5 7: 601 sumn = 7 8: 617 sumn = 5 9: 619 sumn = 7 10: 641 sumn = 2 11: 653 sumn = 5 12: 659 sumn = 2 13: 673 sumn = 7 14: 677 sumn = 2 15: 691 sumn = 7 16: 709 sumn = 7 17: 727 sumn = 7 18: 743 sumn = 5 19: 761 sumn = 5 20: 797 sumn = 5 21: 821 sumn = 2 22: 839 sumn = 2 23: 853 sumn = 7 24: 857 sumn = 2 25: 887 sumn = 5 26: 907 sumn = 7 27: 911 sumn = 2 28: 929 sumn = 2 29: 941 sumn = 5 30: 947 sumn = 2 31: 977 sumn = 5 32: 983 sumn = 2 33: 997 sumn = 7 done...