Prime numbers which contain 123: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|J}}: brief explanation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10:
=={{header|11l}}==
<
I a == 2
R 1B
Line 35:
c++
print()
print(‘Found ’c‘ "123" primes less than 1000000’)</
{{out}}
Line 52:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
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</
{{out}}
<pre>
Line 97:
=={{header|Arturo}}==
<
upTo1M: upTo100K ++ select select 100001..999999 => odd? => prime?
Line 106:
print ""
print ["'123' Numbers < 1000000:" size select upTo1M => contains123?]</
{{out}}
Line 119:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PRIME_NUMBERS_WHICH_CONTAIN_123.AWK
BEGIN {
Line 150:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 164:
=={{header|BASIC256}}==
<
print "Prime numbers which contain 123"
print
Line 201:
endif
next n
end subroutine</
{{out}}
<pre>Same as FreeBASIC entry.</pre>
=={{header|CLU}}==
<
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</
{{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#)].
<
// 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>
{{out}}
<pre>
Line 271:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
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</
{{out}}
<pre>
Line 326:
=={{header|FreeBASIC}}==
<
Dim Shared As Integer column
Line 360:
Print !"\n\n\Encontrados "; column; " n£meros primos por debajo de"; limite
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 382:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 426:
}
fmt.Println("\nFound", count, "such primes under", climit, "\b.")
}</
{{out}}
Line 443:
=={{header|J}}==
<
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</
<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]].
<
def digits: tostring | explode;
Line 468:
(1000000
| "\nThere are \(count(primes_with_123)) \"123\" primes less than \(.).")</
{{out}}
(Abbreviated)
Line 487:
=={{header|Julia}}==
<
function containstringinbase(N, str, base, verbose = true)
Line 498:
containstringinbase(1_000_000, "123", 10, false)
containstringinbase(1_000_000_000, "123", 10, false)
</
<pre>
Found 46 primes < 100000 which contain the string 123 in base 10 representation.
Line 513:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 557:
print ${parr[*]}
print ${#parr[*]} found under $MAX_SHOW
echo ; print ${primecnt} found under $MAX_COUNT</
{{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}}==
<
Length[s1]
Length[Select[Prime[Range[PrimePi[10^6]]], IntegerDigits/*(SequenceCount[#, {1, 2, 3}] &)/*GreaterThan[0]]]</
{{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}}==
<
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."</
{{out}}
Line 624:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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;</
{{out}}
<pre>
Line 644:
=={{header|Phix}}==
<!--<
<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 < 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 < 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>
<!--</
{{out}}
<pre>
Line 660:
=={{header|PureBasic}}==
{{trans|FreeBASIC}}
<
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 705:
PrintN(#CRLF$ + "Found " + Str(column) + " prime numbers below " + Str(limite))
Input()
CloseConsole()</
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 711:
=={{header|Python}}==
{{trans|FreeBASIC}}
<
#!/usr/bin/python
Line 737:
prime(limite, False)
print("\n\nEncontrados ", columna, " números primos por debajo de", limite)
</syntaxhighlight>
{{out}}
<pre>
Line 744:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
put display @p123[^(@p123.first: * > 1e5, :k)];
Line 753:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</
{{out}}
<pre>46 matching:
Line 769:
<br>the number of columns to be shown, and the decimal string that the primes must contain. A negative number for
<br>the number of columns suppresses the list of primes, but shows the total number of primes found.
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 832:
=={{header|Ring}}==
<
load "stdlib.ring"
row = 0
Line 853:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 873:
=={{header|Ruby}}==
<
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>
{{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}}==
<
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}"
}</
{{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.
<
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)</
{{out}}
Line 969:
=={{header|XPL0}}==
<
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.");
]</
{{out}}
Line 1,022:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
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>
{{out}}
<pre>
|