File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ The smallest number expressible as the sum of a prime square, prime cube, and prime
3
+ fourth power is 28. In fact, there are exactly four numbers below fifty that can be
4
+ expressed in such a way:
5
+
6
+ 28 = 22 + 23 + 24
7
+ 33 = 32 + 23 + 24
8
+ 49 = 52 + 23 + 24
9
+ 47 = 22 + 33 + 24
10
+
11
+ How many numbers below fifty million can be expressed as the sum of a prime square,
12
+ prime cube, and prime fourth power?
13
+ """
14
+
15
+
16
+ def solution (limit : int = 50000000 ) -> int :
17
+ """
18
+ Return the number of integers less than limit which can be expressed as the sum
19
+ of a prime square, prime cube, and prime fourth power.
20
+ >>> solution(50)
21
+ 4
22
+ """
23
+ ret = set ()
24
+ prime_square_limit = int ((limit - 24 ) ** (1 / 2 ))
25
+
26
+ primes = set (range (3 , prime_square_limit + 1 , 2 ))
27
+ primes .add (2 )
28
+ for p in range (3 , prime_square_limit + 1 , 2 ):
29
+ if p not in primes :
30
+ continue
31
+ primes .difference_update (set (range (p * p , prime_square_limit + 1 , p )))
32
+
33
+ for prime1 in primes :
34
+ square = prime1 * prime1
35
+ for prime2 in primes :
36
+ cube = prime2 * prime2 * prime2
37
+ if square + cube >= limit - 16 :
38
+ break
39
+ for prime3 in primes :
40
+ tetr = prime3 * prime3 * prime3 * prime3
41
+ total = square + cube + tetr
42
+ if total >= limit :
43
+ break
44
+ ret .add (total )
45
+
46
+ return len (ret )
47
+
48
+
49
+ if __name__ == "__main__" :
50
+ print (solution ())
You can’t perform that action at this time.
0 commit comments