Composite numbers k with no single digit factors whose factors are all substrings of k: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 18:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find composite k with no single digit factors whose factors are all substrings of k #
# returns TRUE if the string representation of f is a substring of k str, FALSE otherwise #
PROC is substring = ( STRING k str, INT f )BOOL:
Line 70:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 79:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">valid?: function [n][
pf: factors.prime n
every? pf 'f ->
Line 95:
]
'i + 2
]</langsyntaxhighlight>
 
{{out}}
Line 112:
=={{header|F_Sharp|F#}}==
Can anything be described as a translation of J? I use a wheel as described in J's comments, but of course I use numerical methods not euyuk! strings.
<langsyntaxhighlight lang="fsharp">
// Composite numbers k with no single digit factors whose factors are all substrings of k. Nigel Galloway: January 28th., 2022
let fG n g=let rec fN i g e l=match i<g,g=0L,i%10L=g%10L with (true,_,_)->false |(_,true,_)->true |(_,_,true)->fN(i/10L)(g/10L) e l |_->fN l e e (l/10L) in fN n g g (n/10L)
Line 118:
Seq.unfold(fun n->Some(n|>List.filter(fun(n:int64)->not(Open.Numeric.Primes.Prime.Numbers.IsPrime &n) && fN n),n|>List.map((+)210L)))([1L..2L..209L]
|>List.filter(fun n->n%3L>0L && n%5L>0L && n%7L>0L))|>Seq.concat|>Seq.skip 1|>Seq.take 20|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 147:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 195:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 204:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> */2 3 5 7
210
#1+I.0=+/|:4 q:1+i.210
48</langsyntaxhighlight>
 
Or: 48 out of every 210 positive numbers have no single digit factors.
Line 213:
So, we can generate a few hundred thousand lists of 48 numbers, discard the primes (and 1), then check what's left using substring matching on the factors. (We allow '0' as a 'factor' in our substring test so that we can work with a padded array of factors, avoiding variable length factor lists.)
 
<langsyntaxhighlight Jlang="j"> 2{._10 ]\(#~ */"1@((+./@(E. '0 ',])~&>)&:(":&.>)q:))(#~ 1-1&p:)}.,(1+I.0=+/|:4 q:1+i.210)+/~210*i.2e5
15317 59177 83731 119911 183347 192413 1819231 2111317 2237411 3129361
5526173 11610313 13436683 13731373 13737841 13831103 15813251 17692313 19173071 28118827</langsyntaxhighlight>
 
Most of the time here is the substring testing, so this could be better optimized.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
using Primes
 
Line 231:
 
foreach(p -> print(lpad(last(p), 9), first(p) == 10 ? "\n" : ""), enumerate(take(20, seq)))
</langsyntaxhighlight>{{out}}
<pre>
15317 59177 83731 119911 183347 192413 1819231 2111317 2237411 3129361
Line 238:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CompositeAndContainsPrimeFactor]
CompositeAndContainsPrimeFactor[k_Integer] := Module[{id, pf},
If[CompositeQ[k],
Line 252:
]
]
out = Select[Range[30000000], CompositeAndContainsPrimeFactor]</langsyntaxhighlight>
{{out}}
<pre>{15317, 59177, 83731, 119911, 183347, 192413, 1819231, 2111317, 2237411, 3129361, 5526173, 11610313, 13436683, 13731373, 13737841, 13831103, 15813251, 17692313, 19173071, 28118827}</pre>
Line 259:
==={{header|Free Pascal}}===
modified [[Factors_of_an_integer#using_Prime_decomposition]]
<langsyntaxhighlight lang="pascal">program FacOfInt;
// gets factors of consecutive integers fast
// limited to 1.2e11
Line 617:
writeln('runtime ',T0/1000:0:3,' s');
end.
</syntaxhighlight>
</lang>
{{out|@TIO.RUN}}
<pre style="height:480px">
Line 818:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl"> use strict;
use warnings;
use ntheory qw<is_prime factor gcd>;
Line 829:
last LOOP if ++$cnt == 20;
}
print $values =~ s/.{1,100}\K/\n/gr;</langsyntaxhighlight>
{{out}}
<pre> 15317 59177 83731 119911 183347 192413 1819231 2111317 2237411 3129361
Line 836:
=={{header|Phix}}==
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span>
Line 866:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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;">"Total time:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(As usual, limiting to the first 10 under pwa/p2js keeps the time staring at a blank screen under 10s)</small>
Line 896:
The obvious problem with the above is that prime_factors() quite literally does not know when to quit.
Output as above, except Total time is reduced to 47s.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span>
Line 933:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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;">"Total time:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from sympy import isprime, factorint
 
def contains_its_prime_factors_all_over_7(n):
Line 952:
if found == 20:
break
</langsyntaxhighlight>{{out}}
<pre>
15,317 59,177 83,731 119,911 183,347 192,413 1,819,231 2,111,317 2,237,411 3,129,361
Line 960:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
use Lingua::EN::Numbers;
 
Line 966:
next if (1 < $_ gcd 210) || .is-prime || any .&prime-factors.map: -> $n { !.contains: $n };
$_
} )[^20].batch(10)».&comma».fmt("%10s").join: "\n";</langsyntaxhighlight>
 
{{out}}
Line 973:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var e = Enumerator({|f|
 
var c = (9.primorial)
Line 993:
say n
break if (--count <= 0)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,012:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,043:
}
Fmt.print("$,10d", res[0..9])
Fmt.print("$,10d", res[10..19])</langsyntaxhighlight>
 
{{out}}
Line 1,053:
=={{header|XPL0}}==
Runs in 33.6 seconds on Raspberry Pi 4.
<langsyntaxhighlight XPL0lang="xpl0">include xpllib; \for ItoA, StrFind and RlOutC
int K, C;
 
Line 1,091:
K:= K+2; \must be odd because all factors > 2 are odd primes
until C >= 20;
]</langsyntaxhighlight>
 
{{out}}
10,327

edits