Primes whose first and last number is 3: Difference between revisions
m →{{header|REXX}}: changed "ending" to "trailing" (decimal digit). |
→More general: Fixed minor bug. |
||
Line 249: | Line 249: | ||
for (x in [1, 2, 3, 5, 7, 9]) { // begins and ends with 'x' |
for (x in [1, 2, 3, 5, 7, 9]) { // begins and ends with 'x' |
||
var primes = getQualifyingPrimes.call(x, d) |
var primes = getQualifyingPrimes.call(x, d) |
||
var len = d + (d/3).floor |
var len = d + ((d-1)/3).floor |
||
Fmt.print("Primes under $,%(len)d which begin and end in $d:", 10.pow(d), x) |
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) |
for (chunk in Lst.chunks(primes, 10)) Fmt.print("$,%(len)d", chunk) |
Revision as of 13:17, 24 July 2021
- Task
Find primes n (in base ten) whose first and last decimal digit is 3, and where n < 4,000.
Show all output here on this page.
- Stretch goal
Find and show only the number of these types of primes that are < 1,000,000.
Go
<lang go>package main
import (
"fmt" "rcu"
)
func main() {
var primes []int candidates := []int{3, 33} for i := 303; i <= 393; i += 10 { candidates = append(candidates, i) } for i := 3003; i <= 3993; i += 10 { candidates = append(candidates, i) } for _, cand := range candidates { if rcu.IsPrime(cand) { primes = append(primes, cand) } } fmt.Println("Primes under 4,000 which begin and end in 3:") for i, p := range primes { fmt.Printf("%5s ", rcu.Commatize(p)) if (i+1)%11 == 0 { fmt.Println() } } fmt.Println("\nFound", len(primes), "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.
Raku
<lang perl6>my $upto = 1e4;
for 1,3,5,7,9 -> $bracket {
print "\nPrimes up to $upto bracketed by $bracket - "; say display ^$upto .grep: { .starts-with($bracket) && .ends-with($bracket) && .is-prime }
}
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n" ) {
cache $list; $title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</lang>
- Output:
Primes up to 10000 bracketed by 1 - 39 matching: 11 101 131 151 181 191 1021 1031 1051 1061 1091 1151 1171 1181 1201 1231 1291 1301 1321 1361 1381 1451 1471 1481 1511 1531 1571 1601 1621 1721 1741 1801 1811 1831 1861 1871 1901 1931 1951 Primes up to 10000 bracketed by 3 - 33 matching: 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 Primes up to 10000 bracketed by 5 - 1 matching: 5 Primes up to 10000 bracketed by 7 - 35 matching: 7 727 757 787 797 7027 7057 7127 7177 7187 7207 7237 7247 7297 7307 7417 7457 7477 7487 7507 7517 7537 7547 7577 7607 7687 7717 7727 7757 7817 7867 7877 7907 7927 7937 Primes up to 10000 bracketed by 9 - 29 matching: 919 929 9029 9049 9059 9109 9199 9209 9239 9319 9349 9419 9439 9479 9539 9619 9629 9649 9679 9689 9719 9739 9749 9769 9829 9839 9859 9929 9949
REXX
This REXX version allows the specification of the limit to search for these types of primes (in base ten), and it also
allows the specification of what (both) the leading and trailing decimal digit must be.
Also, if a negative cols is specified, only the count of primes found is shown. <lang ring>/*REXX pgm finds and displays primes (base ten) that contain a leading and trailing 3. */ parse arg hi cols dig . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 4000 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ if dig== | dig=="," then dig= 3 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 10 /*width of a number in any column. */ title= ' primes N (in base ten) that have a leading and trailing digit of ' dig ,
" where N < " commas(hi)
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') found= 0; idx= 1 /*initialize # of primes found; IDX. */ $= /*list of primes that contain a string.*/
do j=1 for # /*find primes with leading/trailing dig*/ parse var @.j Ld 2 -1 Td /*obtain the leading and trailing digit*/ if Ld\==dig then iterate /*does prime contain a leading DIG ? */ /* ◄■■■■■■■ a filter.*/ if Td\==dig then iterate /* " " " " trailing ? ? */ /* ◄■■■■■■■ a filter.*/ found= found + 1 /*bump the number of primes found. */ if cols<=0 then iterate /*Build the list (to be shown later)? */ c= commas(@.j) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a prime ──► $ list, allow big #*/ if found//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(found) title exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " semaphores. */ #=5; sq.#= @.# **2 /*number of primes so far; prime². */ do j=@.#+2 by 2 to hi-1 /*find odd primes from here on. */ parse var j -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ if j// 3==0 then iterate /*" " " 3? */ if j// 7==0 then iterate /*" " " 7? */ do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/ if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ end /*k*/ /* [↑] only process numbers ≤ √ J */ #= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ end /*j*/; return</lang>
- output when using the default inputs:
index │ primes N (in base ten) that have a leading and trailing digit of 3 where N < 4,000 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 3 313 353 373 383 3,023 3,083 3,163 3,203 3,253 11 │ 3,313 3,323 3,343 3,373 3,413 3,433 3,463 3,533 3,583 3,593 21 │ 3,613 3,623 3,643 3,673 3,733 3,793 3,803 3,823 3,833 3,853 31 │ 3,863 3,923 3,943 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 33 primes N (in base ten) that have a leading and trailing digit of 3 where N < 4,000
- output when using the default inputs of: 1000000 -1
Found 2,251 primes N (in base ten) that have a leading and trailing digit of 3 where N < 1,000,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
Basic task
<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.
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-1)/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>
- Output:
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.
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.