Product of min and max prime factors: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Quackery.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(14 intermediate revisions by 11 users not shown)
Line 1:
{{draft task}}
Exactly as the task title implies.
 
Line 7:
 
 
:''For some unknown reason, the term for '''1''' is defined to be '''1'''.''<br>
:''AnA equal case could be made that it should be '''0''', in'''undefined''', myor opinion.''<br>'-∞'''''. ¯\_(ツ)_/¯
:''A even stronger case that it should be ''''undefined'''' or '''NaN'''.'' ¯\_(ツ)_/¯
 
 
Line 15 ⟶ 14:
;*[[oeis:A066048|OEIS:A066048 - Product of smallest and greatest prime factors of n]]
 
 
=={{header|11l}}==
{{trans|BASIC}}
 
<syntaxhighlight lang="11l">
V m = 100
V c = [0B] * (m + 1)
 
L(p) 2 .. Int(m ^ 0.5)
L(ci) (p * p .. m).step(p)
c[ci] = 1B
 
print(‘ #4’.format(1), end' ‘’)
 
L(i) 2 .. m
L(l) 2 .. i
I !(c[l] | i % l > 0)
L(h) (i .. 2).step(-1)
I !(c[h] | i % h > 0)
print(‘ #4’.format(l * h), end' I i % 10 == 0 {"\n"} E ‘’)
L(l).break
</syntaxhighlight>
 
{{out}}
<pre>
1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10
</pre>
 
=={{header|ALGOL 68}}==
Line 461 ⟶ 496:
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">prints " 1" ; special case 1 since the result is normally null
; for the factors of 1
 
loop 2..100 'i [
f: factors.prime i
prints to :string .format:"5d" mul min f max f
if 0 = i%10 -> print ""
]</syntaxhighlight>
{{out}}
<pre> 1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|BASIC}}==
Line 856 ⟶ 912:
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|D}}==
<syntaxhighlight lang="D">
import std.stdio : writef;
 
void main() {
auto sieve = sieve(100);
for(ulong i = 1; i <= 100; i++) {
writef("%4d ", min_prime_factor(i, sieve) * max_prime_factor(i, sieve));
if(i % 10 == 0)
writef("\n");
}
}
bool []sieve(ulong max) {
bool []sieve = new bool[](max + 1);
sieve[] = true;
sieve[0] = false;
sieve[1] = false;
 
for(ulong i = 2; i <= max; i++)
if(sieve[i])
for(ulong j = i * 2; j <= max; j += i)
sieve[j] = false;
 
return sieve;
}
ulong min_prime_factor(ulong number, bool []sieve) {
if (number <= 1)
return 1;
 
for(ulong index = 2; index <= number; index++)
if(sieve[index] && number % index == 0)
return index;
 
assert(0 && "Sieve was not initialized correctly");
}
ulong max_prime_factor(ulong number, bool []sieve) {
if (number <= 1)
return 1;
 
for(ulong index = number; index >= 2; index--)
if(sieve[index] && number % index == 0)
return index;
 
assert(0 && "Sieve was not initialized correctly");
}
 
</syntaxhighlight>
{{out}}
<pre>
1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Another example of code reuse by creating subroutines to that get used for more than one Rosetta Code task.
 
<syntaxhighlight lang="Delphi">
procedure StoreNumber(N: integer; var IA: TIntegerDynArray);
{Expand and store number in array}
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=N;
end;
 
 
procedure GetPrimeFactors(N: integer; var Facts: TIntegerDynArray);
{Get all the prime factors of a number}
var I: integer;
begin
I:=2;
SetLength(Facts,0);
repeat
begin
if (N mod I) = 0 then
begin
StoreNumber(I,Facts);
N:=N div I;
end
else I:=GetNextPrime(I);
end
until N=1;
end;
 
 
 
procedure ProductMinMaxFactors(Memo: TMemo);
var I,Cnt,P: integer;
var IA: TIntegerDynArray;
var S: string;
begin
Cnt:=1;
S:=' 1';
for I:=2 to 100 do
begin
GetPrimeFactors(I,IA);
P:=IA[0] * IA[High(IA)];
Inc(Cnt);
S:=S+Format('%5D',[P]);
If (Cnt mod 10)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10
 
Count = 100
Elapsed Time: 2.961 ms.
</pre>
 
 
=={{header|Draco}}==
Line 1,333 ⟶ 1,522:
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strformat
 
func primeFactors(n: Positive): seq[Natural] =
if n == 1: return @[1]
var n = n
var d = 2
while d * d <= n:
if n mod d == 0:
result.add d
while n mod d == 0:
n = n div d
inc d
if n != 1: result.add n
 
for n in 1..100:
let pf = n.primeFactors
stdout.write &"{pf[0] * pf[^1]:4}"
stdout.write if n mod 10 == 0: '\n' else: ' '
</syntaxhighlight>
 
{{out}}
<pre> 1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10
</pre>
 
 
=={{header|Perl}}==
Line 1,480 ⟶ 1,704:
 
=={{header|Python}}==
===Procedural===
<syntaxhighlight lang="python">''' Rosetta code rosettacode.org/wiki/Product_of_min_and_max_prime_factors '''
 
Line 1,504 ⟶ 1,729:
91 46 93 94 95 6 9409 14 33 10
</pre>
 
===Functional===
Defining an infinite series, tabulating with flexible column widths for larger samples,
 
and writing (rather than importing) a primeFactors function:
 
<syntaxhighlight lang="python">'''Produce of min and max prime factors'''
 
from itertools import chain, count, islice
from math import floor, sqrt
 
 
# oeisA066048 :: [Int]
def oeisA066048():
'''Infinite series of terms in OEIS A066048.
'''
def f(x):
ns = primeFactors(x)
return ns[0] * ns[-1]
 
return chain([1], map(f, count(2)))
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 100 terms in OEIS A066048.
'''
print(table(10)(
list(map(
str, islice(
oeisA066048(),
100
)
))
))
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divisible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# primeFactors :: Int -> [Int]
def primeFactors(n):
'''A list of the prime factors of n.
'''
def f(qr):
r = qr[1]
return step(r), 1 + r
 
def step(x):
return 1 + (x << 2) - ((x >> 1) << 1)
 
def go(x):
root = floor(sqrt(x))
 
def p(qr):
q = qr[0]
return root < q or 0 == (x % q)
 
q = until(p)(f)(
(2 if 0 == x % 2 else 3, 1)
)[0]
return [x] if q > root else [q] + go(x // q)
 
return go(n)
 
 
# table :: Int -> [String] -> String
def table(n):
'''A list of strings formatted as
right-justified rows of n columns.
'''
def go(xs):
w = len(max(xs, key=len))
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
s.rjust(w, ' ') for s in xs
])
)
return go
 
 
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
def go(f):
def g(x):
v = x
while not p(v):
v = f(v)
return v
return g
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre> 1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|Quackery}}==
Line 1,554 ⟶ 1,902:
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
=={{header|RPL}}==
{{works with|HP|49g}}
≪ {1}
2 100 '''FOR''' j
j FACTORS
DUP 1 GET
SWAP REVLIST 2 GET
* +
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
{{out}
<pre>
1: {1 4 9 4 25 6 49 4 9 10 121 6 169 14 15 4 289 6 361 10 21 22 529 6 25 26 9 14 841 10 961 4 33 34 35 6 1369 38 39 10 1681 14 1849 22 15 46 2209 6 49 10 51 26 2809 6 55 14 57 58 3481 10 3721 62 21 4 65 22 4489 34 69 14 5041 6 5329 74 15 38 77 26 6241 10 9 82 6889 14 85 86 87 22 7921 10 91 46 93 94 95 6 9409 14 33 10}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby" line>require 'prime'
 
res = [1]+ (2..100).map{|n| n.prime_division.map(&:first).minmax.inject(&:*)}
res.each_slice(10){|slice| puts '%5d'*slice.size % slice}</syntaxhighlight>
{{out}}
<pre> 1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby" line>1..100 -> map {|n| lpf(n) * gpf(n) }.slices(10).each {|a|
a.map{ '%5s' % _ }.join(' ').say
}</syntaxhighlight>
 
{{out}}
<pre>
1 4 9 4 25 6 49 4 9 10
121 6 169 14 15 4 289 6 361 10
21 22 529 6 25 26 9 14 841 10
961 4 33 34 35 6 1369 38 39 10
1681 14 1849 22 15 46 2209 6 49 10
51 26 2809 6 55 14 57 58 3481 10
3721 62 21 4 65 22 4489 34 69 14
5041 6 5329 74 15 38 77 26 6241 10
9 82 6889 14 85 86 87 22 7921 10
91 46 93 94 95 6 9409 14 33 10
</pre>
 
=={{header|VTL-2}}==
Line 1,601 ⟶ 1,999:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
9,476

edits