mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Solves problem 13
Prepares problem 14 (hummm, though one it seems)
This commit is contained in:
16
e_13.py
16
e_13.py
@@ -11,9 +11,21 @@
|
|||||||
"""
|
"""
|
||||||
def ten_dig_sum(filename):
|
def ten_dig_sum(filename):
|
||||||
"""
|
"""
|
||||||
|
Returns the first 10 digits of the sum of all numbers in filename.
|
||||||
"""
|
"""
|
||||||
|
return int(str(sum(load_data(filename)))[0:10])
|
||||||
|
|
||||||
return 1
|
def load_data(filename):
|
||||||
|
"""
|
||||||
|
Loads the data from a file into a table
|
||||||
|
"""
|
||||||
|
file = open(filename, "r")
|
||||||
|
data = []
|
||||||
|
for line in file :
|
||||||
|
data.append(int(line))
|
||||||
|
file.close()
|
||||||
|
return data
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print "Answer : %d" % (fun())
|
#data = ten_dig_sum("e_13.data")
|
||||||
|
print "Answer : %d" % (ten_dig_sum("e_13.data"))
|
||||||
|
|||||||
34
e_14.py
Executable file
34
e_14.py
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
##---
|
||||||
|
# jlengrand
|
||||||
|
#Created on : Mon Jan 16 10:22:33 CET 2012
|
||||||
|
#
|
||||||
|
# DESCRIPTION : Solves problem 14 of Project Euler
|
||||||
|
The following iterative sequence is defined for the set of positive integers:
|
||||||
|
|
||||||
|
n -> n/2 (n is even)
|
||||||
|
n -> 3n + 1 (n is odd)
|
||||||
|
|
||||||
|
Using the rule above and starting with 13, we generate the following sequence:
|
||||||
|
13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||||
|
It can be seen that this sequence (starting at 13 and finishing at 1) contains
|
||||||
|
10 terms.
|
||||||
|
Although it has not been proved yet (Collatz Problem), it is thought that all
|
||||||
|
starting numbers finish at 1.
|
||||||
|
|
||||||
|
Which starting number, under one million, produces the longest chain?
|
||||||
|
|
||||||
|
NOTE: Once the chain starts the terms are allowed to go above one million.
|
||||||
|
##---
|
||||||
|
"""
|
||||||
|
def longest_chain(max_value):
|
||||||
|
"""
|
||||||
|
Returns the starting number under max_value that produces the longest
|
||||||
|
chain given the sequence.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print "Answer : %d" % (longest_chain(1000000))
|
||||||
Reference in New Issue
Block a user