Primes whose first and last number is 3: Difference between revisions
Added Wren |
|||
Line 36: | Line 36: | ||
Found 33 numbers |
Found 33 numbers |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
{{libheader|Wren-trait}} |
|||
{{libheader|Wren-seq}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "/math" for Int |
|||
import "/trait" for Stepped |
|||
import "/seq" for Lst |
|||
import "/fmt" for Fmt |
|||
var primes = [] |
|||
for (seq in [ 3..3, 33..33, Stepped.new(303..393, 10), Stepped.new(3003..3993, 10) ]) { |
|||
for (e in seq) if (Int.isPrime(e)) primes.add(e) |
|||
} |
|||
System.print("Primes under 4,000 which begin and end in 3:") |
|||
for (chunk in Lst.chunks(primes, 11)) Fmt.print("$,5d", chunk) |
|||
System.print("\nFound %(primes.count) such primes.")</lang> |
|||
{{out}} |
|||
<pre> |
|||
Primes under 4,000 which begin and end in 3: |
|||
3 313 353 373 383 3,023 3,083 3,163 3,203 3,253 3,313 |
|||
3,323 3,343 3,373 3,413 3,433 3,463 3,533 3,583 3,593 3,613 3,623 |
|||
3,643 3,673 3,733 3,793 3,803 3,823 3,833 3,853 3,863 3,923 3,943 |
|||
Found 33 such primes. |
|||
</pre> |
</pre> |
||
Revision as of 10:13, 24 July 2021
- 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%10 = 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 3 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...
Wren
<lang ecmascript>import "/math" for Int import "/trait" for Stepped import "/seq" for Lst import "/fmt" for Fmt
var primes = [] for (seq in [ 3..3, 33..33, Stepped.new(303..393, 10), Stepped.new(3003..3993, 10) ]) {
for (e in seq) if (Int.isPrime(e)) primes.add(e)
} System.print("Primes under 4,000 which begin and end in 3:") for (chunk in Lst.chunks(primes, 11)) Fmt.print("$,5d", chunk) System.print("\nFound %(primes.count) such primes.")</lang>
- Output:
Primes under 4,000 which begin and end in 3: 3 313 353 373 383 3,023 3,083 3,163 3,203 3,253 3,313 3,323 3,343 3,373 3,413 3,433 3,463 3,533 3,583 3,593 3,613 3,623 3,643 3,673 3,733 3,793 3,803 3,823 3,833 3,853 3,863 3,923 3,943 Found 33 such primes.
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.