Prime numbers which contain 123

From Rosetta Code
Revision as of 08:53, 12 July 2021 by PureFox (talk | contribs) (Added Go)
Prime numbers which contain 123 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 those prime numbers 'n' whose decimal representation contains the consecutive digits 123, where n < 100,000.




Go

Translation of: Wren
Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"
   "strings"

)

func main() {

   const limit = 100_000
   primes := rcu.Primes(limit)
   var results []int
   for _, p := range primes {
       if p < 1000 {
           continue
       }
       ps := fmt.Sprintf("%s", p)
       if strings.Contains(ps, "123") {
           results = append(results, p)
       }
   }
   climit := rcu.Commatize(limit)
   fmt.Printf("Primes under %s which contain '123' when expressed in decimal:\n", climit)
   for i, p := range results {
       fmt.Printf("%7s ", rcu.Commatize(p))
       if (i+1)%10 == 0 {
           fmt.Println()
       }
   }
   fmt.Println("\n\nFound", len(results), "such primes.")

}</lang>

Output:
Primes under 100,000 which contain '123' when expressed in decimal:
  1,123   1,231   1,237   8,123  11,239  12,301  12,323  12,329  12,343  12,347 
 12,373  12,377  12,379  12,391  17,123  20,123  22,123  28,123  29,123  31,123 
 31,231  31,237  34,123  37,123  40,123  41,231  41,233  44,123  47,123  49,123 
 50,123  51,239  56,123  59,123  61,231  64,123  65,123  70,123  71,233  71,237 
 76,123  81,233  81,239  89,123  91,237  98,123 

Found 46 such primes.

Julia

<lang julia>using Primes

function containstringinbase(N, str, base)

   arr = filter(n -> occursin(str, string(n, base=base)), primes(N))
   println("Found $(length(arr)) primes < $N which contain the string $str in base $base representation:")
   foreach(p -> print(rpad(p[2], 6), p[1] % 12 == 0 ? "\n" : ""), enumerate(arr))

end

containstringinbase(100_000, "123", 10)

</lang>

Output:
Found 46 primes < 100000 which contain the string 123 in base 10 representation:
1123  1231  1237  8123  11239 12301 12323 12329 12343 12347 12373 12377
12379 12391 17123 20123 22123 28123 29123 31123 31231 31237 34123 37123
40123 41231 41233 44123 47123 49123 50123 51239 56123 59123 61231 64123
65123 70123 71233 71237 76123 81233 81239 89123 91237 98123

Ring

<lang ring> load "stdlib.ring" row = 0

see "working..." + nl see "Prime numbers which contain 123 are:" + nl

for n = 1 to 100000

   strn = string(n)
   ind = substr(strn,"123")
   if isprime(n) and ind > 0
      see "" + n + " "
      row++
      if row%5 = 0
         see nl
      ok
   ok  

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>

Output:
working...
Prime numbers which contain 123 are:
1123 1231 1237 8123 11239 
12301 12323 12329 12343 12347 
12373 12377 12379 12391 17123 
20123 22123 28123 29123 31123 
31231 31237 34123 37123 40123 
41231 41233 44123 47123 49123 
50123 51239 56123 59123 61231 
64123 65123 70123 71233 71237 
76123 81233 81239 89123 91237 
98123 
Found 46 numbers
done...

Wren

Library: Wren-math
Library: Wren-fmt
Library: Wren-seq

<lang ecmascript>import "/math" for Int import "/fmt" for Fmt import "/seq" for Lst

var limit = 1e5 var primes = Int.primeSieve(limit).where { |p| p > 999 } var results = [] for (p in primes) {

   if (p.toString.contains("123")) results.add(p)

} Fmt.print("Primes under $,d which contain '123' when expressed in decimal:", limit) for (chunk in Lst.chunks(results, 10)) Fmt.print("$,7d", chunk) System.print("\nFound %(results.count) such primes.")</lang>

Output:
Primes under 100,000 which contain '123' when expressed in decimal:
  1,123   1,231   1,237   8,123  11,239  12,301  12,323  12,329  12,343  12,347
 12,373  12,377  12,379  12,391  17,123  20,123  22,123  28,123  29,123  31,123
 31,231  31,237  34,123  37,123  40,123  41,231  41,233  44,123  47,123  49,123
 50,123  51,239  56,123  59,123  61,231  64,123  65,123  70,123  71,233  71,237
 76,123  81,233  81,239  89,123  91,237  98,123

Found 46 such primes.