Jump to content

Practical numbers: Difference between revisions

m
syntax highlighting fixup automation
(Added 11l)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F properDivisors(n)
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.’)</langsyntaxhighlight>
 
{{out}}
Line 71:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">pract ← ∧/⍳∊(+⌿⊢×[1](2/⍨≢)⊤(⍳2*≢))∘(⍸0=⍳|⊢)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight APLlang="apl"> ⍸pract¨⍳333 ⍝ Which numbers from 1 to 333 are practical?
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</langsyntaxhighlight>
 
=={{header|C#|CSharp}}==
<langsyntaxhighlight lang="csharp">using System.Collections.Generic; using System.Linq; using static System.Console;
 
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); } }</langsyntaxhighlight>
{{out}}
<pre> 1 2 4 6 8 12 16 18 20 24
Line 117:
=={{header|C++}}==
{{trans|Phix}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <numeric>
Line 192:
<< "a practical number.\n";
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 210:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">sub make_divisors( n as uinteger, div() as uinteger )
'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</langsyntaxhighlight>
 
All practical numbers up to and including the stretch goal of DCLXVI.
Line 265:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 321:
fmt.Println(" The final ten are", practical[len(practical)-10:])
fmt.Println("\n666 is practical:", isPractical(666))
}</langsyntaxhighlight>
 
{{out}}
Line 337:
Borrowed from the [[Proper divisors#J]] page:
 
<langsyntaxhighlight Jlang="j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. ]</langsyntaxhighlight>
 
Borrowed from the [[Power set#J]] page:
 
<langsyntaxhighlight Jlang="j">ps=: #~ 2 #:@i.@^ #</langsyntaxhighlight>
 
Implementation:
 
<langsyntaxhighlight Jlang="j">isPrac=: ('' -:&# i. -. 0,+/"1@(ps ::empty)@properDivisors)"0</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> +/ isPrac 1+i.333 NB. count practical numbers
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</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Phix}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class PracticalNumbers {
Line 455:
return str.toString();
}
}</langsyntaxhighlight>
 
{{out}}
Line 479:
The version of `proper_divisors` at [[Proper_divisors#jq]] may be used and therefore its definition
is not repeated here.
<langsyntaxhighlight lang="jq"># A reminder to include the definition of `proper_divisors`
# include "proper_divisors" { search: "." };
 
Line 532:
task(333),
(666,6666,66666
| "\nIs \(.) practical? \(if isPractical then "Yes." else "No." end)" )</langsyntaxhighlight>
{{out}}
<pre>
Line 550:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">using Primes
 
""" proper divisors of n """
Line 586:
println("$n is ", ispractical(n) ? "" : "not ", "a practical number.")
end
</langsyntaxhighlight>{{out}}
<pre>
There are 77 practical numbers between 1 to 333 inclusive.
Line 598:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import intsets, math, sequtils, strutils
 
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."</langsyntaxhighlight>
 
{{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>
<langsyntaxhighlight lang="pascal">program practicalnumbers;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 838:
{$IFNDEF UNIX} readln; {$ENDIF}
end.
</syntaxhighlight>
</lang>
{{out}}
<pre> TIO.RUN.
Line 871:
==={{header|alternative}}===
Now without generating sum of allset.
<langsyntaxhighlight lang="pascal">program practicalnumbers2;
 
{$IFDEF FPC}
Line 1,003:
{$ENDIF}
end.
</syntaxhighlight>
</lang>
{{out}}
<pre> TIO.RUN
Line 1,030:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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;</langsyntaxhighlight>
{{out}}
<pre>77 matching numbers:
Line 1,074:
=={{header|Phix}}==
{{trans|Python|(the composition of functions version)}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,119:
===Python: Straight forward implementation===
 
<langsyntaxhighlight lang="python">from itertools import chain, cycle, accumulate, combinations
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.")</langsyntaxhighlight>
 
{{out}}
Line 1,192:
An extra check on the sum of all factors has a minor positive effect too.
 
<langsyntaxhighlight lang="python">def is_practical5(x: int) -> bool:
"""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.")</langsyntaxhighlight>
 
{{out}}
Line 1,250:
===Composition of pure functions===
 
<langsyntaxhighlight lang="python">'''Practical numbers'''
 
from itertools import accumulate, chain, groupby, product
Line 1,417:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>77 OEIS A005153 numbers in [1..333]
Line 1,434:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Prime::Factor:ver<0.3.0+>;
 
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;</langsyntaxhighlight>
{{out}}
<pre>77 matching numbers:
Line 1,468:
=={{header|Rust}}==
{{trans|Phix}}
<langsyntaxhighlight lang="rust">fn sum_of_any_subset(n: isize, f: &[isize]) -> bool {
let len = f.len();
if len == 0 {
Line 1,571:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,587:
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby">say is_practical(2**128 + 1) #=> false
say is_practical(2**128 + 4) #=> true</langsyntaxhighlight>
 
Slow implementation (as the task requires):
<langsyntaxhighlight lang="ruby">func is_practical(n) {
 
var set = Set()
Line 1,612:
for n in ([666, 6666, 66666]) {
say "#{'%5s' % n } is practical? #{is_practical(n)}"
}</langsyntaxhighlight>
 
Efficient algorithm:
<langsyntaxhighlight lang="ruby">func is_practical(n) {
 
n.is_odd && return (n == 1)
Line 1,629:
 
return true
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,642:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Collections.Generic, System.Linq, System.Console
 
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</langsyntaxhighlight>
{{out}}
Same as C#
Line 1,673:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int, Nums
 
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))")</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.