File tree 4 files changed +14
-28
lines changed
4 files changed +14
-28
lines changed Original file line number Diff line number Diff line change 1
- val sieve = helpers. Sieve ( 100 )
1
+ val N = 10000 - 1
2
2
3
- val result = (2 until 10000 ).filter { i =>
4
- val other = (sieve.sumOfDivisors(i) - i).toInt
5
- i != other && i == sieve.sumOfDivisors(other) - other
3
+ val sieve = sieves.DivisorSum (3 * N )
4
+
5
+ val result = (2 to N ).filter { i =>
6
+ val other = sieve(i) - i
7
+ i != other && i == sieve(other) - other
6
8
}
7
9
8
10
println(result.sum)
Original file line number Diff line number Diff line change 1
1
import scala .collection .Searching ._
2
2
3
- val sieve = helpers. Sieve ( 168 )
3
+ val N = 28123
4
4
5
- val abundant = (1 to 28123 ).filter( i =>
6
- sieve.sumOfDivisors(i) > 2 * i
5
+ val sieve = sieves.DivisorSum (N )
6
+
7
+ val abundant = (1 to N ).filter( i =>
8
+ sieve(i) > 2 * i
7
9
)
8
10
9
- val result = (1 to 28123 ).filter( i =>
11
+ val result = (1 to N ).filter( i =>
10
12
abundant.takeWhile(_ < i).forall( j =>
11
13
abundant.search(i - j) match {
12
14
case Found (_) => false
Original file line number Diff line number Diff line change 1
1
val N = 1000000
2
- val sieve = helpers. Sieve ( 1000 )
2
+ val sieve = sieves. DivisorSum ( N )
3
3
4
4
var seen = Set (1 )
5
5
var longest = - 1
@@ -12,7 +12,7 @@ var result = 0
12
12
seen += i
13
13
step += 1
14
14
chain += i -> step
15
- i = sieve.sumOfDivisors (i).toInt - i
15
+ i = sieve(i) - i
16
16
}
17
17
if (chain.contains(i)) {
18
18
val length = step - chain(i) + 1
Original file line number Diff line number Diff line change @@ -51,24 +51,6 @@ case class Sieve(n: Int) {
51
51
52
52
53
53
54
- def sumOfDivisors (num : Int ): Long = {
55
- var n = num
56
- var result = 1L
57
- primes.takeWhile( p =>
58
- n != 1 && p* p <= n
59
- ).foreach { p =>
60
- var cur = p
61
- while (n % p == 0 ) {
62
- n /= p
63
- cur *= p
64
- }
65
- if (cur != p)
66
- result *= (cur - 1 )/ (p - 1 )
67
- }
68
- if (n != 1 )
69
- result *= (n.toLong* n - 1 )/ (n - 1 )
70
- result
71
- }
72
54
73
55
def numberOfDivisors (num : Int , power : Int = 1 ): Int = {
74
56
var n = num
You can’t perform that action at this time.
0 commit comments