Prime numbers which contain 123: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|J}}: brief explanation)
m (syntax highlighting fixup automation)
Line 10:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 35:
c++
print()
print(‘Found ’c‘ "123" primes less than 1000000’)</langsyntaxhighlight>
 
{{out}}
Line 52:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find primes whose decimal representation contains 123 #
INT max prime = 1 000 000;
# sieve the primes to max prime #
Line 83:
OD;
print( ( newline, "Found ", whole( p123 count, 0 ), " ""123"" primes below ", whole( UPB prime, 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 97:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">upTo100K: select select 2..99999 => odd? => prime?
upTo1M: upTo100K ++ select select 100001..999999 => odd? => prime?
 
Line 106:
 
print ""
print ["'123' Numbers < 1000000:" size select upTo1M => contains123?]</langsyntaxhighlight>
 
{{out}}
Line 119:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIME_NUMBERS_WHICH_CONTAIN_123.AWK
BEGIN {
Line 150:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 164:
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">global columna
print "Prime numbers which contain 123"
print
Line 201:
endif
next n
end subroutine</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">isqrt = proc (s: int) returns (int)
x0: int := s/2
if x0=0 then return(s) end
Line 246:
stream$putl(po, "\nThere are " || int$unparse(count)
|| " primes that contain '123' below 1,000,000.")
end start_up</langsyntaxhighlight>
{{out}}
<pre> 1123 1231 1237 8123 11239 12301 12323 12329 12343 12347
Line 257:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)].
<langsyntaxhighlight lang="fsharp">
// Numbers containing 123. Nigel Galloway: July 14th., 2021
let rec fN g=if g%1000=123 then true else if g<1230 then false else fN(g/10)
primes32()|>Seq.takeWhile((>)100000)|>Seq.filter fN|>Seq.iter(printf "%d "); printfn ""
printfn "Count to 1 million is %d" (primes32()|>Seq.takeWhile((>)1000000)|>Seq.filter fN|>Seq.length)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 271:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping io kernel literals math
math.functions math.functions.integer-logs math.primes
math.statistics sequences sequences.extras sequences.product
Line 306:
unzip cum-sum [ commas ] map swap zip
[ "Found %7s such primes under %s.\n" printf ] assoc-each
] time</langsyntaxhighlight>
{{out}}
<pre>
Line 326:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim Shared As Integer column
 
Line 360:
Print !"\n\n\Encontrados "; column; " n£meros primos por debajo de"; limite
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 382:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 426:
}
fmt.Println("\nFound", count, "such primes under", climit, "\b.")
}</langsyntaxhighlight>
 
{{out}}
Line 443:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> p:I.1 2 3 +./@E."1/ 10 #.inv p:i.p:inv 1e5 NB. primes less than 1e5 containing decimal digit sequence 1 2 3
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
+/1 2 3 +./@E."1/ 10 #.inv p:i.p:inv 1e6 NB. count of primes less than 1e6 containing decimal digit sequence 1 2 3
451</langsyntaxhighlight>
 
<code>p:inv 1e5</code> is 9592 -- the number of primes less than 1e5. <code>p:i.9592</code> enumerates those primes (first is 2, last is 99991). <code>10 #.inv</code> converts this to the corresponding table of decimal digits (<code>0 0 0 0 2</code> for the first row, <code>9 9 9 9 1</code> for the last row). <code>1 2 3 +./@E."1 </code> identifies those rows containing the sequence <code>1 2 3</code>, and <code>I.</code> gets the indices of those rows and, finally, <code>p:</code> gets the prime numbers corresponding to those indices. Similarly, <code>+/</code> counts the rows with suitable primes.
Line 455:
 
For a suitable implementation of `is_prime`, see e.g. # [[Erd%C5%91s-primes#jq]].
<langsyntaxhighlight lang="jq">def count(stream): reduce stream as $i (0; .+1);
 
def digits: tostring | explode;
Line 468:
 
(1000000
| "\nThere are \(count(primes_with_123)) \"123\" primes less than \(.).")</langsyntaxhighlight>
{{out}}
(Abbreviated)
Line 487:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function containstringinbase(N, str, base, verbose = true)
Line 498:
containstringinbase(1_000_000, "123", 10, false)
containstringinbase(1_000_000_000, "123", 10, false)
</langsyntaxhighlight>{{out}}
<pre>
Found 46 primes < 100000 which contain the string 123 in base 10 representation.
Line 513:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 557:
print ${parr[*]}
print ${#parr[*]} found under $MAX_SHOW
echo ; print ${primecnt} found under $MAX_COUNT</langsyntaxhighlight>
{{out}}<pre>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
Line 566:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">s1 = Select[Prime[Range[PrimePi[10^5]]], IntegerDigits/*(SequenceCount[#, {1, 2, 3}] &)/*GreaterThan[0]]
Length[s1]
 
Length[Select[Prime[Range[PrimePi[10^6]]], IntegerDigits/*(SequenceCount[#, {1, 2, 3}] &)/*GreaterThan[0]]]</langsyntaxhighlight>
{{out}}
<pre>{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}
Line 576:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
const N = 1_000_000 - 1 # Sieve of Erathostenes size.
Line 609:
var count = 0
for _ in primes123(1_000_000): inc count
echo "Found ", count, " “123” primes less than 1_000_000."</langsyntaxhighlight>
 
{{out}}
Line 624:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 632:
my @hundredthousand = grep /123/, @{ primes(1e5) };
my $million = grep /123/, @{ primes(1e6) };
print "@hundredthousand\n\nmillion count is $million\n" =~ s/.{70}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 644:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">m123</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"123"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 651:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d &lt; 100_000: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d &lt; 1_000_000\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 660:
=={{header|PureBasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 705:
PrintN(#CRLF$ + "Found " + Str(column) + " prime numbers below " + Str(limite))
Input()
CloseConsole()</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 711:
=={{header|Python}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="python">
#!/usr/bin/python
 
Line 737:
prime(limite, False)
print("\n\nEncontrados ", columna, " números primos por debajo de", limite)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 744:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @p123 = ^∞ .grep: { .is-prime && .contains: 123 };
 
put display @p123[^(@p123.first: * > 1e5, :k)];
Line 753:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>46 matching:
Line 769:
<br>the number of columns to be shown, &nbsp; and the decimal string that the primes must contain. &nbsp; A negative number for
<br>the number of columns suppresses the list of primes, &nbsp; but shows the total number of primes found.
<langsyntaxhighlight lang="rexx">/*REXX program finds & displays primes (in decimal) that contain the decimal digits 123.*/
parse arg hi cols str . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 100000 /*Not specified? Then use the default.*/
Line 811:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # of Ps; assign next P; P square*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 832:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
row = 0
Line 853:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 873:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
RE = /123/
puts Prime.each(100_000).select {|prime| RE.match? prime.to_s}.join(" "), ""
puts "#{Prime.each(1_000_000).count {|prime| RE.match? prime.to_s} } 123-primes below 1 million."
</syntaxhighlight>
</lang>
{{out}}
<pre>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
Line 887:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func numbers_with_subdigits(upto, base = 10, s = 123.digits(base)) {
Enumerator({|callback|
for k in (0 .. base**(upto.len(base) - s.len)) {
Line 917:
var count = numbers_with_subdigits(10**n).grep { .is_prime }.len
say "Found #{'%6s' % count.commify} such primes < 10^#{n}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 939:
{{libheader|Wren-seq}}
The only number under 1,000 which can possibly satisfy the task description is 123 and that's clearly divisible by 3 and hence composite.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/seq" for Lst
Line 952:
limit = 1e6
var count = primes.count { |p| p.toString.contains("123") }
Fmt.print("\nFound $,d such primes under $,d.", count, limit)</langsyntaxhighlight>
 
{{out}}
Line 969:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,007:
];
CrLf(0); IntOut(0, Count); Text(0, " ^"123^" primes found below 1,000,000.");
]</langsyntaxhighlight>
 
{{out}}
Line 1,022:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">
sub isPrime(v)
if v < 2 then return False : fi
Line 1,059:
print "\n\nEncontrados ", columna, " n£meros primos por debajo de ", limite
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
10,327

edits