Primes whose first and last number is 3: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
(Added Go)
Line 4: Line 4:


<br><br>
<br><br>

=={{header|Go}}==
{{libheader|Go-rcu}}
<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>

{{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>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 10:31, 24 July 2021

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



Go

Library: Go-rcu

<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.

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

Library: Wren-math
Library: Wren-trait
Library: Wren-seq
Library: 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>

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.