Jump to content

Amicable pairs: Difference between revisions

→‎{{header|Julia}}: Just learned, that we can use headlines to mark alternative versions.
(→‎{{header|Julia}}: truncated the output, skipping results 10 to 299)
(→‎{{header|Julia}}: Just learned, that we can use headlines to mark alternative versions.)
Line 3,137:
</syntaxhighlight>
 
{{out}}
Using the <code>factor()</code> function from the <code>Primes</code> package allows for a quicker calculation, especially when it comes to big numbers. Here we use a busy one-liner with an iterator. The following code prints the amicable pairs in ascending order and also prints the sum of the amicable pair and the cumulative sum of all pairs found so far; this allows to check results, when solving [https://projecteuler.net/problem=21 Project Euler problem #21].
Note, the output is ''not'' ordered by the first figure, see e.g. counters 11, 15, ..., 139, 141, etc.
<pre>
Amicable pairs not greater than 20000000
1 => 220, 284
2 => 1184, 1210
3 => 2620, 2924
4 => 5020, 5564
5 => 6232, 6368
6 => 10744, 10856
7 => 12285, 14595
8 => 17296, 18416
9 => 66928, 66992
10 => 67095, 71145
11 => 63020, 76084
12 => 69615, 87633
13 => 79750, 88730
14 => 122368, 123152
15 => 100485, 124155
16 => 122265, 139815
[...]
138 => 18655744, 19154336
139 => 16871582, 19325698
140 => 17844255, 19895265
141 => 17754165, 19985355
</pre>
 
 
===Alternative===
 
Using the <code>factor()</code> function from the <code>Primes</code> package allows for a quicker calculation, especially when it comes to big numbers. Here we use a busy one-liner with an iterator. The following code prints the amicable pairs in ''ascending order'' and also prints the sum of the amicable pair and the cumulative sum of all pairs found so far; this allows to check results, when solving [https://projecteuler.net/problem=21 Project Euler problem #21].
 
<syntaxhighlight lang="julia">
Line 3,149 ⟶ 3,179:
 
count = 0
cummsumcumsum = 0
 
println("count, a, b, a+b, Sum(a+b)")
Line 3,180 ⟶ 3,210:
8, 17296, 18416, 35712, 115818
9, 63020, 76084, 139104, 254922
10, 66928, 66992, 133920, 388842
11, 67095, 71145, 138240, 527082
12, 69615, 87633, 157248, 684330
13, 79750, 88730, 168480, 852810
14, 100485, 124155, 224640, 1077450
15, 122265, 139815, 262080, 1339530
16, 122368, 123152, 245520, 1585050
[...]
300, 189406984, 203592056, 392999040, 31530421032
3

edits

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