File tree Expand file tree Collapse file tree 2 files changed +97
-0
lines changed Expand file tree Collapse file tree 2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Project Euler Problem 1: https://projecteuler.net/problem=1
3
+
4
+ Multiples of 3 and 5
5
+
6
+ If we list all the natural numbers below 10 that are multiples of 3 or 5,
7
+ we get 3, 5, 6 and 9. The sum of these multiples is 23.
8
+
9
+ Find the sum of all the multiples of 3 or 5 below 1000.
10
+ """
11
+
12
+
13
+ def solution (n : int = 1000 ) -> int :
14
+ """
15
+ Returns the sum of all the multiples of 3 or 5 below n.
16
+
17
+ >>> solution(3)
18
+ 0
19
+ >>> solution(4)
20
+ 3
21
+ >>> solution(10)
22
+ 23
23
+ >>> solution(600)
24
+ 83700
25
+ """
26
+
27
+ result = 0
28
+ for i in range (n ):
29
+ if i % 3 == 0 :
30
+ result += i
31
+ elif i % 5 == 0 :
32
+ result += i
33
+ return result
34
+
35
+
36
+ if __name__ == "__main__" :
37
+ print (f"{ solution () = } " )
Original file line number Diff line number Diff line change
1
+ """
2
+ Problem 14: https://projecteuler.net/problem=14
3
+
4
+ Problem Statement:
5
+ The following iterative sequence is defined for the set of positive integers:
6
+
7
+ n → n/2 (n is even)
8
+ n → 3n + 1 (n is odd)
9
+
10
+ Using the rule above and starting with 13, we generate the following sequence:
11
+
12
+ 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
13
+
14
+ It can be seen that this sequence (starting at 13 and finishing at 1) contains
15
+ 10 terms. Although it has not been proved yet (Collatz Problem), it is thought
16
+ that all starting numbers finish at 1.
17
+
18
+ Which starting number, under one million, produces the longest chain?
19
+ """
20
+
21
+
22
+ def solution (n : int = 1000000 ) -> int :
23
+ """Returns the number under n that generates the longest sequence using the
24
+ formula:
25
+ n → n/2 (n is even)
26
+ n → 3n + 1 (n is odd)
27
+
28
+ # The code below has been commented due to slow execution affecting Travis.
29
+ # >>> solution(1000000)
30
+ # 837799
31
+ >>> solution(200)
32
+ 171
33
+ >>> solution(5000)
34
+ 3711
35
+ >>> solution(15000)
36
+ 13255
37
+ """
38
+ largest_number = 0
39
+ pre_counter = 0
40
+
41
+ for input1 in range (n ):
42
+ counter = 1
43
+ number = input1
44
+
45
+ while number > 1 :
46
+ if number % 2 == 0 :
47
+ number /= 2
48
+ counter += 1
49
+ else :
50
+ number = (3 * number ) + 1
51
+ counter += 1
52
+
53
+ if counter > pre_counter :
54
+ largest_number = input1
55
+ pre_counter = counter
56
+ return largest_number
57
+
58
+
59
+ if __name__ == "__main__" :
60
+ print (solution (int (input ().strip ())))
You can’t perform that action at this time.
0 commit comments