-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P41.scala
34 lines (32 loc) · 1.11 KB
/
P41.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package arithmetic
// P41 (**) A list of Goldbach compositions.
// Given a range of integers by its lower and upper limit, print a list of
// all even numbers and their Goldbach composition.
//
// scala> printGoldbachList(9 to 20)
// 10 = 3 + 7
// 12 = 5 + 7
// 14 = 3 + 11
// 16 = 3 + 13
// 18 = 5 + 13
// 20 = 3 + 17
//
// In most cases, if an even number is written as the sum of two prime
// numbers, one of them is very small. Very rarely, the primes are both
// bigger than, say, 50. Try to find out how many such cases there are in
// the range 2..3000.
//
// Example (minimum value of 50 for the primes):
// scala> printGoldbachListLimited(1 to 2000, 50)
// 992 = 73 + 919
// 1382 = 61 + 1321
// 1856 = 67 + 1789
// 1928 = 61 + 1867
object P41:
// This implementation isn't very efficient because the primes are generated
// eagerly. It works much better in Haskell where list is lazy.
def goldbachList(a: Int, b: Int): Map[Int, (Int, Int)] =
(a to b)
.collect:
case x if x % 2 == 0 => (x, P40.goldbach(x))
.toMap