Prime numbers which contain 123: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 18: Line 18:
</lang>{{out}}
</lang>{{out}}
<pre>
<pre>
Found 46 positive integers < 100000 which contain the string 123 in base 10 representation:
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
1123 1231 1237 8123 11239 12301 12323 12329 12343 12347 12373 12377
12379 12391 17123 20123 22123 28123 29123 31123 31231 31237 34123 37123
12379 12391 17123 20123 22123 28123 29123 31123 31231 31237 34123 37123

Revision as of 07:01, 12 July 2021

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 prime numbers which contain 123, where n < 100,000




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