Prime numbers which contain 123: Difference between revisions

Content added Content deleted
(Prime numbers which contain 123 en Python)
(Add Factor)
Line 58: Line 58:
</pre>
</pre>


=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: assocs assocs.extras formatting grouping io kernel
literals math math.functions math.functions.integer-logs
math.primes math.statistics sequences sequences.extras
sequences.product sorting tools.memory.private tools.time ;

<<
CONSTANT: d { 0 1 2 3 4 5 6 7 8 9 } ! digits that can be anywhere
CONSTANT: e { 1 3 5 7 9 } ! digits that can be at the end
>>

CONSTANT: digits {
${ { 1 } { 2 } { 3 } d d d d d e }
${ d { 1 } { 2 } { 3 } d d d d e }
${ d d { 1 } { 2 } { 3 } d d d e }
${ d d d { 1 } { 2 } { 3 } d d e }
${ d d d d { 1 } { 2 } { 3 } d e }
${ d d d d d { 1 } { 2 } { 3 } e }
${ d d d d d d { 1 } { 2 } { 3 } }
}

: candidates ( -- seq )
digits [ <product-sequence> ] map-concat
[ <reversed> 0 [ 10^ * + ] reduce-index ] map ;

: 123primes ( -- assoc )
candidates [ prime? ] filter
[ integer-log10 1 + ] collect-by >alist natural-sort ;

[
"Decimal primes under 100,000 which contain '123':" print
123primes dup [ 4 of ] [ 5 of append ] bi natural-sort
10 group [ [ commas "%8s" printf ] each nl ] each nl
[ [ 10^ commas ] [ length ] bi* ] assoc-map
unzip cum-sum [ commas ] map zip assoc-invert
[ "Found %7s such primes under %s.\n" printf ] assoc-each
] time</lang>
{{out}}
<pre>
Decimal primes under 100,000 which contain '123':
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 4 such primes under 10,000.
Found 46 such primes under 100,000.
Found 451 such primes under 1,000,000.
Found 4,412 such primes under 10,000,000.
Found 43,548 such primes under 100,000,000.
Found 435,853 such primes under 1,000,000,000.
Running time: 23.838093371 seconds
</pre>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==