Amicable pairs: Difference between revisions

m
→‎{{header|J}}: add a little explanation
m (→‎{{header|J}}: cleaner implementation)
m (→‎{{header|J}}: add a little explanation)
Line 2,756:
<syntaxhighlight lang="j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. -.&1</syntaxhighlight>
 
(properDivisors is all factors except "the number itself when that number is greater than 1".)
 
Amicable pairs:
Line 2,768 ⟶ 2,770:
12285 14595
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}}==
6,962

edits