Primes whose first and last number is 3: Difference between revisions
Content added Content deleted
m (added a stretch goal to this (draft) task.) |
(→{{header|Wren}}: Added a more general version.) |
||
Line 199: | Line 199: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
===Basic task=== |
|||
<lang ecmascript>import "/math" for Int |
<lang ecmascript>import "/math" for Int |
||
import "/trait" for Stepped |
import "/trait" for Stepped |
||
Line 220: | Line 222: | ||
Found 33 such primes. |
Found 33 such primes. |
||
</pre> |
|||
===More general=== |
|||
This version deals with primes (in base 10) beginning and ending with any specified digit and with up to a given number of digits. |
|||
<lang ecmascript>import "/math" for Int |
|||
import "/trait" for Stepped |
|||
import "/seq" for Lst |
|||
import "/fmt" for Fmt |
|||
var getQualifyingPrimes = Fn.new { |x, d| |
|||
if (d.type != Num || !d.isInteger || d < 1) Fiber.abort("Invalid number of digits.") |
|||
if (x == 2 || x == 5) return [x] |
|||
if (x % 2 == 0) return [] |
|||
var primes = [] |
|||
var candidates = [x] |
|||
for (i in 1...d) { |
|||
var pow = 10.pow(i) |
|||
var start = x * (pow + 1) |
|||
var end = start + pow - 10 |
|||
candidates.addAll(Stepped.new(start..end, 10).toList) |
|||
} |
|||
return candidates.where { |cand| Int.isPrime(cand) }.toList |
|||
} |
|||
var d = 4 // up to 'd' digits |
|||
for (x in [1, 2, 3, 5, 7, 9]) { // begins and ends with 'x' |
|||
var primes = getQualifyingPrimes.call(x, d) |
|||
var len = d + (d/3).floor |
|||
Fmt.print("Primes under $,%(len)d which begin and end in $d:", 10.pow(d), x) |
|||
for (chunk in Lst.chunks(primes, 10)) Fmt.print("$,%(len)d", chunk) |
|||
System.print("\nFound %(primes.count) such primes.\n") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Primes under 10,000 which begin and end in 1: |
|||
11 101 131 151 181 191 1,021 1,031 1,051 1,061 |
|||
1,091 1,151 1,171 1,181 1,201 1,231 1,291 1,301 1,321 1,361 |
|||
1,381 1,451 1,471 1,481 1,511 1,531 1,571 1,601 1,621 1,721 |
|||
1,741 1,801 1,811 1,831 1,861 1,871 1,901 1,931 1,951 |
|||
Found 39 such primes. |
|||
Primes under 10,000 which begin and end in 2: |
|||
2 |
|||
Found 1 such primes. |
|||
Primes under 10,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. |
|||
Primes under 10,000 which begin and end in 5: |
|||
5 |
|||
Found 1 such primes. |
|||
Primes under 10,000 which begin and end in 7: |
|||
7 727 757 787 797 7,027 7,057 7,127 7,177 7,187 |
|||
7,207 7,237 7,247 7,297 7,307 7,417 7,457 7,477 7,487 7,507 |
|||
7,517 7,537 7,547 7,577 7,607 7,687 7,717 7,727 7,757 7,817 |
|||
7,867 7,877 7,907 7,927 7,937 |
|||
Found 35 such primes. |
|||
Primes under 10,000 which begin and end in 9: |
|||
919 929 9,029 9,049 9,059 9,109 9,199 9,209 9,239 9,319 |
|||
9,349 9,419 9,439 9,479 9,539 9,619 9,629 9,649 9,679 9,689 |
|||
9,719 9,739 9,749 9,769 9,829 9,839 9,859 9,929 9,949 |
|||
Found 29 such primes. |
|||
</pre> |
</pre> |
||