Primes whose first and last number is 3
Primes whose first and last number is 3 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 primes n whose first and last number is 3, where n < 4,000
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Primes whose first and last number is 3" + nl row = 0
for n = 1 to 4000
strn = string(n) if left(strn,1) = "3" and right(strn,1) = "3" and isprime(n) see "" + n + " " row++ if row%5 = 0 see nl ok ok
next
see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>
- Output:
working... Primes whose first and last number is 1 3 313 353 373 383 3023 3083 3163 3203 3253 3313 3323 3343 3373 3413 3433 3463 3533 3583 3593 3613 3623 3643 3673 3733 3793 3803 3823 3833 3853 3863 3923 3943 Found 33 numbers done...
XPL0
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number int N, I; [if N <= 1 then return false; for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true; ];
func Has3s(N); \Return 'true' if first and last digits are 3 int N; [N:= N/10; if rem(0) # 3 then return false; while N do N:= N/10; return rem(0) = 3; ];
int Count, N; [Count:= 0; for N:= 0 to 4000-1 do
if Has3s(N) & IsPrime(N) then [IntOut(0, N); Count:= Count+1; if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\); ];
CrLf(0); IntOut(0, Count); Text(0, " such primes found below 4000. "); ]</lang>
- Output:
3 313 353 373 383 3023 3083 3163 3203 3253 3313 3323 3343 3373 3413 3433 3463 3533 3583 3593 3613 3623 3643 3673 3733 3793 3803 3823 3833 3853 3863 3923 3943 33 such primes found below 4000.