Abundant, deficient and perfect number classifications: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎Python: Reduce: Added a variant expressed in terms of nthArrow)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 24:
*   [[Proper divisors]]
*   [[Amicable pairs]]
 
 
=={{header|11l}}==
Line 859 ⟶ 858:
perfect: 4
abundant: 4953</pre>
 
=={{header|Elena}}==
{{trans|C#}}
Line 1,241:
Abundant: 4953
</pre>
 
=={{header|GFA Basic}}==
 
Line 1,359 ⟶ 1,360:
perfect=4
abundant=4953</pre>
 
=={{header|Haskell}}==
<lang Haskell>divisors :: (Integral a) => a -> [a]
Line 1,590 ⟶ 1,592:
perfect: 4
abundant: 4953</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
The definition of proper_divisors is taken from [[Proper_divisors#jq]]:
<lang jq># unordered
def proper_divisors:
. as $n
| if $n > 1 then 1,
( range(2; 1 + (sqrt|floor)) as $i
| if ($n % $i) == 0 then $i,
(($n / $i) | if . == $i then empty else . end)
else empty
end)
else empty
end;</lang>
'''The task:'''
<lang jq>def sum(stream): reduce stream as $i (0; . + $i);
 
def classify:
. as $n
| sum(proper_divisors)
| if . < $n then "deficient" elif . == $n then "perfect" else "abundant" end;
 
reduce (range(1; 20001) | classify) as $c ({}; .[$c] += 1 )</lang>
{{out}}
<lang sh>$ jq -n -c -f AbundantDeficientPerfect.jq
{"deficient":15043,"perfect":4,"abundant":4953}</lang>
 
=={{header|Jsish}}==
Line 1,682 ⟶ 1,711:
</code>
 
=={{header|jqK}}==
<lang K>
{{works with|jq|1.4}}
/Classification of numbers into abundant, perfect and deficient
The definition of proper_divisors is taken from [[Proper_divisors#jq]]:
/ numclass.k
<lang jq># unordered
def proper_divisors:
. as $n
| if $n > 1 then 1,
( range(2; 1 + (sqrt|floor)) as $i
| if ($n % $i) == 0 then $i,
(($n / $i) | if . == $i then empty else . end)
else empty
end)
else empty
end;</lang>
'''The task:'''
<lang jq>def sum(stream): reduce stream as $i (0; . + $i);
 
/return 0,1 or -1 if perfect or abundant or deficient respectively
def classify:
numclass: {s:(+/&~x!'!1+x)-x; :[s>x;:1;:[s<x;:-1;:0]]}
. as $n
/classify numbers from 1 to 20000 into respective groups
| sum(proper_divisors)
c: =numclass' 1+!20000
| if . < $n then "deficient" elif . == $n then "perfect" else "abundant" end;
/print statistics
 
`0: ,"Deficient = ", $(#c[0])
reduce (range(1; 20001) | classify) as $c ({}; .[$c] += 1 )</lang>
`0: ,"Perfect = ", $(#c[1])
`0: ,"Abundant = ", $(#c[2])
</lang>
{{out}}
<pre>
<lang sh>$ jq -n -c -f AbundantDeficientPerfect.jq
Deficient = 15043
{"deficient":15043,"perfect":4,"abundant":4953}</lang>
Perfect = 4
Abundant = 4953
 
</pre>
 
=={{header|Kotlin}}==
Line 1,744 ⟶ 1,768:
Perfect = 4
Abundant = 4953
</pre>
 
=={{header|K}}==
<lang K>
/Classification of numbers into abundant, perfect and deficient
/ numclass.k
 
/return 0,1 or -1 if perfect or abundant or deficient respectively
numclass: {s:(+/&~x!'!1+x)-x; :[s>x;:1;:[s<x;:-1;:0]]}
/classify numbers from 1 to 20000 into respective groups
c: =numclass' 1+!20000
/print statistics
`0: ,"Deficient = ", $(#c[0])
`0: ,"Perfect = ", $(#c[1])
`0: ,"Abundant = ", $(#c[2])
</lang>
{{out}}
<pre>
Deficient = 15043
Perfect = 4
Abundant = 4953
 
</pre>
 
Line 1,928 ⟶ 1,930:
Abundant 4953
</pre>
 
=={{header|ML}}==
==={{header|mLite}}===
Line 2,374 ⟶ 2,377:
$h{div_sum($_) <=> $_}++ for 1..20000;
say "Perfect: $h{0} Deficient: $h{-1} Abundant: $h{1}";</lang>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.12}}
<lang perl6>sub propdivsum (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
unless x % d { @l.push: d; my \y = x div d; @l.push: y if y != d }
}
sum @l
}
 
say bag (1..20000).map: { propdivsum($_) <=> $_ }</lang>
{{out}}
<pre>Bag(Less(15043), More(4953), Same(4))</pre>
 
=={{header|Phix}}==
Line 2,975 ⟶ 2,964:
4953 abundant numbers
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.12}}
<lang perl6>sub propdivsum (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
unless x % d { @l.push: d; my \y = x div d; @l.push: y if y != d }
}
sum @l
}
 
say bag (1..20000).map: { propdivsum($_) <=> $_ }</lang>
{{out}}
<pre>Bag(Less(15043), More(4953), Same(4))</pre>
 
=={{header|REXX}}==
Line 3,119 ⟶ 3,123:
next
</lang>
 
=={{header|Ruby}}==
With [[proper_divisors#Ruby]] in place:
<lang ruby>res = Hash.new(0)
(1 .. 20_000).each{|n| res[n.proper_divisors.sum <=> n] += 1}
puts "Deficient: #{res[-1]} Perfect: #{res[0]} Abundant: #{res[1]}"
</lang>
{{out}}<pre>
Deficient: 15043 Perfect: 4 Abundant: 4953
</pre>
 
=={{header|Rust}}==
Line 3,149 ⟶ 3,163:
perfect: 4
abundant: 4953
</pre>
 
=={{header|Ruby}}==
With [[proper_divisors#Ruby]] in place:
<lang ruby>res = Hash.new(0)
(1 .. 20_000).each{|n| res[n.proper_divisors.sum <=> n] += 1}
puts "Deficient: #{res[-1]} Perfect: #{res[0]} Abundant: #{res[1]}"
</lang>
{{out}}<pre>
Deficient: 15043 Perfect: 4 Abundant: 4953
</pre>
 
Line 3,656 ⟶ 3,660:
classified.len()-perfect-abundant, perfect, abundant));</lang>
{{out}}<pre>Deficient=15043, perfect=4, abundant=4953</pre>
 
 
=={{header|ZX Spectrum Basic}}==
10,333

edits