Abundant, deficient and perfect number classifications: Difference between revisions
Content deleted Content added
Walterpachl (talk | contribs) m →REXX version 2: tag correction |
Added Bracmat solution |
||
Line 85: | Line 85: | ||
Deficient: 15043 |
Deficient: 15043 |
||
</pre> |
</pre> |
||
=={{header|Bracmat}}== |
|||
Two solutions are given. The first solution first decomposes the current number into a multiset of prime factors and then constructs the proper divisors. The second solution finds proper divisors by checking all candidates from 1 up to the square root of the given number. The first solution is a few times faster, because establishing the prime factors of a small enough number (less than 2^32 or less than 2^64, depending on the bitness of Bracmat) is fast. |
|||
<lang bracmat>( clk$:?t0 |
|||
& ( multiples |
|||
= prime multiplicity |
|||
. !arg:(?prime.?multiplicity) |
|||
& !multiplicity:0 |
|||
& 1 |
|||
| !prime^!multiplicity*(.!multiplicity) |
|||
+ multiples$(!prime.-1+!multiplicity) |
|||
) |
|||
& ( P |
|||
= primeFactors prime exp poly S |
|||
. !arg^1/67:?primeFactors |
|||
& ( !primeFactors:?^1/67&0 |
|||
| 1:?poly |
|||
& whl |
|||
' ( !primeFactors:%?prime^?exp*?primeFactors |
|||
& !poly*multiples$(!prime.67*!exp):?poly |
|||
) |
|||
& -1+!poly+1:?poly |
|||
& 1:?S |
|||
& ( !poly |
|||
: ? |
|||
+ (#%@?s*?&!S+!s:?S&~) |
|||
+ ? |
|||
| 1/2*!S |
|||
) |
|||
) |
|||
) |
|||
& 0:?deficient:?perfect:?abundant |
|||
& 0:?n |
|||
& whl |
|||
' ( 1+!n:~>20000:?n |
|||
& P$!n |
|||
: ( <!n&1+!deficient:?deficient |
|||
| !n&1+!perfect:?perfect |
|||
| >!n&1+!abundant:?abundant |
|||
) |
|||
) |
|||
& out$(deficient !deficient perfect !perfect abundant !abundant) |
|||
& clk$:?t1 |
|||
& out$(flt$(!t1+-1*!t0,2) sec) |
|||
& clk$:?t2 |
|||
& ( P |
|||
= f h S |
|||
. 0:?f |
|||
& 0:?S |
|||
& whl |
|||
' ( 1+!f:?f |
|||
& !f^2:~>!n |
|||
& ( !arg*!f^-1:~/:?g |
|||
& !S+!f:?S |
|||
& ( !g:~!f&!S+!g:?S |
|||
| |
|||
) |
|||
| |
|||
) |
|||
) |
|||
& 1/2*!S |
|||
) |
|||
& 0:?deficient:?perfect:?abundant |
|||
& 0:?n |
|||
& whl |
|||
' ( 1+!n:~>20000:?n |
|||
& P$!n |
|||
: ( <!n&1+!deficient:?deficient |
|||
| !n&1+!perfect:?perfect |
|||
| >!n&1+!abundant:?abundant |
|||
) |
|||
) |
|||
& out$(deficient !deficient perfect !perfect abundant !abundant) |
|||
& clk$:?t3 |
|||
& out$(flt$(!t3+-1*!t2,2) sec) |
|||
);</lang> |
|||
Output: |
|||
<pre>deficient 15043 perfect 4 abundant 4953 |
|||
4,27*10E0 sec |
|||
deficient 15043 perfect 4 abundant 4953 |
|||
1,63*10E1 sec</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |