Product of min and max prime factors: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(added Arturo implementation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 8 users not shown)
Line 1:
{{draft task}}
Exactly as the task title implies.
 
Line 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 876 ⟶ 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,353 ⟶ 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,698 ⟶ 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,745 ⟶ 1,999:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
9,476

edits