Practical numbers: Difference between revisions
m
syntax highlighting fixup automation
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
{{trans|Nim}}
<
V result = [1]
L(i) 2 .. Int(sqrt(n))
Line 53:
print(‘Found ’count‘ practical numbers between 1 and 333.’)
print()
print(‘666 is ’(I isPractical(666) {‘’} E ‘not ’)‘a practical number.’)</
{{out}}
Line 71:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<
1 2 4 6 8 12 16 18 20 24 28 30 32 36 40 42 48 54 56 60 64 66 72 78 80 84 88 90 96 100 104 108 112 120 126 128 132 140 144
150 156 160 162 168 176 180 192 196 198 200 204 208 210 216 220 224 228 234 240 252 256 260 264 270 272 276 280 288
294 300 304 306 308 312 320 324 330
pract 666 ⍝ Is 666 practical?
1</
=={{header|C#|CSharp}}==
<
class Program {
Line 99:
Write("\nFound {0} practical numbers between 1 and {1} inclusive.\n", c, m);
do Write("\n{0,5} is a{1}practical number.",
m = m < 500 ? m << 1 : m * 10 + 6, ip(m) ? " " : "n im"); while (m < 1e4); } }</
{{out}}
<pre> 1 2 4 6 8 12 16 18 20 24
Line 117:
=={{header|C++}}==
{{trans|Phix}}
<
#include <iostream>
#include <numeric>
Line 192:
<< "a practical number.\n";
return 0;
}</
{{out}}
Line 210:
=={{header|FreeBASIC}}==
<
'produces a list of an integer's proper divisors
for i as uinteger = n/2 to 1 step -1
Line 255:
for n as uinteger = 2 to 666
if is_practical(n) then print n;" ";
next n:print</
All practical numbers up to and including the stretch goal of DCLXVI.
Line 265:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 321:
fmt.Println(" The final ten are", practical[len(practical)-10:])
fmt.Println("\n666 is practical:", isPractical(666))
}</
{{out}}
Line 337:
Borrowed from the [[Proper divisors#J]] page:
<
properDivisors=: factors -. ]</
Borrowed from the [[Power set#J]] page:
<
Implementation:
<
Task examples:
<
77
(#~ isPrac) 1+i.333 NB. list them
1 2 4 6 8 12 16 18 20 24 28 30 32 36 40 42 48 54 56 60 64 66 72 78 80 84 88 90 96 100 104 108 112 120 126 128 132 140 144 150 156 160 162 168 176 180 192 196 198 200 204 208 210 216 220 224 228 234 240 252 256 260 264 270 272 276 280 288 294 300 304 306 30...
isPrac 666 NB. test
1</
=={{header|Java}}==
{{trans|Phix}}
<
public class PracticalNumbers {
Line 455:
return str.toString();
}
}</
{{out}}
Line 479:
The version of `proper_divisors` at [[Proper_divisors#jq]] may be used and therefore its definition
is not repeated here.
<
# include "proper_divisors" { search: "." };
Line 532:
task(333),
(666,6666,66666
| "\nIs \(.) practical? \(if isPractical then "Yes." else "No." end)" )</
{{out}}
<pre>
Line 550:
=={{header|Julia}}==
{{trans|Python}}
<
""" proper divisors of n """
Line 586:
println("$n is ", ispractical(n) ? "" : "not ", "a practical number.")
end
</
<pre>
There are 77 practical numbers between 1 to 333 inclusive.
Line 598:
=={{header|Nim}}==
<
func properDivisors(n: int): seq[int] =
Line 627:
echo "Found ", count, " practical numbers between 1 and 333."
echo()
echo "666 is ", if 666.isPractical: "" else: "not ", "a practical number."</
{{out}}
Line 648:
because the inequality above holds for each of its prime factors:
3 ≤ σ(2) + 1 = 4, 29 ≤ σ(2 × 3^2) + 1 = 40, and 823 ≤ σ(2 × 3^2 × 29) + 1 = 1171. </pre>
<
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 838:
{$IFNDEF UNIX} readln; {$ENDIF}
end.
</syntaxhighlight>
{{out}}
<pre> TIO.RUN.
Line 871:
==={{header|alternative}}===
Now without generating sum of allset.
<
{$IFDEF FPC}
Line 1,003:
{$ENDIF}
end.
</syntaxhighlight>
{{out}}
<pre> TIO.RUN
Line 1,030:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,057:
say @pn . " matching numbers:\n" . (sprintf "@{['%4d' x @pn]}", @pn) =~ s/(.{40})/$1\n/gr;
say '';
printf "%6d is practical? %s\n", $_, is_practical($_) ? 'True' : 'False' for 666, 6666, 66666;</
{{out}}
<pre>77 matching numbers:
Line 1,074:
=={{header|Phix}}==
{{trans|Python|(the composition of functions version)}}
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">sum_of_any_subset</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- return true if any subset of f sums to n.</span>
Line 1,103:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">666</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6666</span><span style="color: #0000FF;">,</span><span style="color: #000000;">66666</span><span style="color: #0000FF;">,</span><span style="color: #000000;">672</span><span style="color: #0000FF;">,</span><span style="color: #000000;">720</span><span style="color: #0000FF;">},</span><span style="color: #000000;">stretch</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,119:
===Python: Straight forward implementation===
<
from typing import List, Tuple
Line 1,174:
print(' ', str(p[:10])[1:-1], '...', str(p[-10:])[1:-1])
x = 666
print(f"\nSTRETCH GOAL: {x} is {'not ' if not is_practical(x) else ''}Practical.")</
{{out}}
Line 1,192:
An extra check on the sum of all factors has a minor positive effect too.
<
"""Practical number test with factor reverse sort and short-circuiting."""
Line 1,229:
print(f"\nSTRETCH GOAL: {x} is {'not ' if not is_practical(x) else ''}Practical.")
x = 5184
print(f"\nEXTRA GOAL: {x} is {'not ' if not is_practical(x) else ''}Practical.")</
{{out}}
Line 1,250:
===Composition of pure functions===
<
from itertools import accumulate, chain, groupby, product
Line 1,417:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>77 OEIS A005153 numbers in [1..333]
Line 1,434:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
sub is-practical ($n) {
Line 1,448:
given [ (1..333).hyper(:32batch).grep: { is-practical($_) } ];
printf "%5s is practical? %s\n", $_, .&is-practical for 666, 6666, 66666, 672, 720;</
{{out}}
<pre>77 matching numbers:
Line 1,468:
=={{header|Rust}}==
{{trans|Phix}}
<
let len = f.len();
if len == 0 {
Line 1,571:
}
}
}</
{{out}}
Line 1,587:
=={{header|Sidef}}==
Built-in:
<
say is_practical(2**128 + 4) #=> true</
Slow implementation (as the task requires):
<
var set = Set()
Line 1,612:
for n in ([666, 6666, 66666]) {
say "#{'%5s' % n } is practical? #{is_practical(n)}"
}</
Efficient algorithm:
<
n.is_odd && return (n == 1)
Line 1,629:
return true
}</
{{out}}
<pre>
Line 1,642:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 1,667:
Write(vbLf & "{0,5} is a{1}practical number.", m, If(ip(m), " ", "n im")) : Loop While m < 1e4
End Sub
End Module</
{{out}}
Same as C#
Line 1,673:
=={{header|Wren}}==
{{libheader|Wren-math}}
<
var powerset // recursive
Line 1,710:
System.print(" The first ten are %(practical[0..9])")
System.print(" The final ten are %(practical[-10..-1])")
System.print("\n666 is practical: %(isPractical.call(666))")</
{{out}}
|