Amicable pairs: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: cleaner implementation)
m (→‎{{header|J}}: add a little explanation)
Line 2,756: Line 2,756:
<syntaxhighlight lang="j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
<syntaxhighlight lang="j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. -.&1</syntaxhighlight>
properDivisors=: factors -. -.&1</syntaxhighlight>

(properDivisors is all factors except "the number itself when that number is greater than 1".)


Amicable pairs:
Amicable pairs:
Line 2,768: Line 2,770:
12285 14595
12285 14595
17296 18416</syntaxhighlight>
17296 18416</syntaxhighlight>

Explanation: We generate sequence of positive integers, starting from 1, and compare each of them to the sum of proper divisors of each of them. Then we fold this comparison diagonally, keeping only the values where the comparison was equal both ways and the smaller value appears before the larger value. Finally, indices into true values give us our amicable pairs.


=={{header|Java}}==
=={{header|Java}}==