We need to compute the largest prime factor of a particular number - in this case 600851475143
We define a new prime factors generator which generates all the prime factors of a particular number (not including 1). After that it is trivially easy to use the max function around its results to find the maximum prime factor.
To generate the prime factors we use a while loop on the number itself being greater than 1. The reason we do that is that once we find a prime factor, we divide the number by the prime factor, and continue further processing with the number being now set to the quotient of the division.
One usually only needs to search within 1 and the square root of a number to find a factor (since one of any two factors of a number is always less than the square root of the number). Since we use it in the range operator (which is non inclusive of the upper end) we add 1.0 to it.
However there are situations when the number itself is a prime number. We would like to return the number in such a case - but that is clearly not an option when the for loop continues only until the square root of the number. Hence we use the chain function from the itertools library to chain a sequence with the number itself at the end of the for loop (which itself is another generator). The chain operator effectively creates a continuous sequence spanning the first and the second generator. We only take the first element from this combined sequence and yield it (as a generated value). That value is the lowest prime factor for the number. Having yielded the prime factor, we divide the number by that factor and resume processing on the quotient.
fromitertoolsimportchain# A function which just takes the first value from a generator# and ignores the restdeffirst(gen):try:returngen.next()exceptStopIteration:returnNone# A generator to return the prime factors of a numberdefprime_factors(n):whilen>1:ff=first(valforvalinchain(xrange(2,int(n**0.5+1.0)),[n])ifn%val==0)yieldffn=n/ff# Actual computationprintmax(prime_factors(600851475143))
After some rather satisfying experience with my blog /var/log/mind, I am starting a codeblog. Why a different one ? Well, the first one focuses much more on opinions and commentaries on various aspects related to software engineering. A lot of times I work on sample code which I think would be useful to be shared - code snippets basically. However these situations occur far more frequently than me being able to articulate an often rather lengthy opinion on some matter. Frequent code postings into that blog would take focus away from its current character and may not help to satisfy its current audience.
Hence this codeblog. The focus here is simple. Code. Whatever example code I believe might be useful to be shared, I shall be posting on this blog. So I do hope you enjoy this as much as I look forward to contributing.
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
This is essentially a requirement to compute the least common multiple for the values 1 through 20.
We first need to find the prime factors for each number. For some of the numbers, some of the prime factors occur more than once. eg. for 12, the prime factors are 2 and 3, of which while 3 occurs once, 2 occurs twice. Thus for each number we create a hashmap of the prime factors and the number of occurrences. To do so we define a inc_count(dict_,key) which increments the occurence count of the key in the dictionary. This dictionary for each number is computed once and is referred to as new_factors.
We need to ensure that we eventually create yet another dictionary which keeps track of the maximum count for each factor across all the numbers. We define yet another dictionary factors which is used to keep track of the maximum occurences of a given factor across all the new_factor instances.
We finally fold the factors dictionary by compute a product of all the factors with each factor being used as many times as it occurs in the factors dictionary. That gives us the least common multiple, which is the solution to the problem.
fromitertoolsimportchain# function to take the first value of a generator and ignore the restdeffirst(gen):try:returngen.next()exceptStopIteration:returnNone# generator to return all the prime factors of a given numberdefprime_factors(n):whilen>1:ff=first(valforvalinchain(xrange(2,int(n**0.5+1.0)),[n])ifn%val==0)yieldffn=n/ff# increment the occurrences value of a key in a dictionarydefinc_count(dict_,key):dict_[key]=dict_[key]+1returndict_# keep track of the maximum occurrences of a key in a dictionarydefset_max_count(dict_,(key,val)):ifdict_[key]<val:dict_[key]=valreturndict_# Actual solution# Initialise a dictionary with all keys with occurrences set to zerofactors=dict((n,0)forninrange(2,21))# For each number for whom we are computing the least common multiplefornuminrange(2,21):# Compute the prime factor occurences dictionary for the numbernew_factors=reduce(inc_count,prime_factors(num),dict((n,0)forninrange(2,21)))# Update the tracking dictionary to keep track # of the maximum occurrences of a key (factor)factors=reduce(set_max_count,new_factors.items(),factors)# Generate a product by multiplying all the factors number=reduce(lambdanum,(key,val):num*(key**val),factors.items(),1)printnumber
ProjectEuler is a wonderful project which provides a number of problems to be solved by writing code. I happened to be attempting to enhance my skills at writing more functional programming oriented code. One of the pages I ran into suggested using the ProjectEuler problems as a way to build and exercise ability to write code using functional programming constructs.
As I started solving these exercises, I realised I probably had the most useful set of problems that would help me build the skill I wanted to. The language of choice for the solutions is python. However python does allow you to write using multiple paradigms - traditional structured, object oriented or even functional. Since I was familiar with the first two and wanting to build capability in the last, it took a great degree of effort to early on write solutions using functional constructs (and eschewing object oriented solutions that I am so comfortable with).
I have found these exercises most helfpul and am sharing my experiences and solutions in the hope that the readers may benefit from some of these and that we can have an intelligent conversation around some of the solutions and their appropriateness in the comment stream. I am not an expert in functional programming, so am more than willing to stand corrected. Just drop a note in the comments and I shall learn from your opinions and thoughts as well, and may even modify some of the solutions should it seem necessary.
Note that project euler problems are not specific to either functional programming or python. They are just as useful to alternative programming styles or other programmiing languages. Functional constructs using python is merely my implementation choice for solving these problems.
This post and all the subsequent solutions can be reviewed by following the tag projecteuler
The various multiples of either 3 and / or 5 between 1 and 20 are 3, 5, 6, 9, 10, 12, 15 and 18. The sum of all these values is 78. Similarly we are required to find the sum of all the multiples of 3 and / or 5 between 1 and 1000.
We use a simple list comprehension using a for loop with a if condition as filter and add up all the elements in the sequence using the sum function.
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
In this case we shall write a method fib() for generating a fibonacci sequence. However in order to keep the sequence open ended (infinite), we shall use a generator. This allows us to use the necessary memory only on demand. However we do need to terminate the sequence generation at some point. In this case when the generated numbers exceed four million. For this we define a method until(gen,predicate) which is also a generator which wraps another generator (in this case fib()), but also accepts a predicate which acts as a stop condition for further generation when the predicate evaluates to True. We supply the predicate itself as a lambda which has a condition check for value exceeding four million. Finally in order to add only the even values in the series, we use the for loop on the generator followed by an if condition to test whether the generated value is even.
fromitertoolsimporttakewhiledeffib():""" Fibonacci series generator """x=1y=1whileTrue:x,y=y,x+yyieldx# Actual Solutionprintsum(valforvalintakewhile(lambdax:x<=4000000,fib())ifval%2==0)
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers.
This is a relatively simple list comprehension problem. Probably the only interesting aspect here is the way a number is checked for being a palindrome - ie. the test str(x*y) == str(x*y)[::-1]
For testing for palindrome we convert the number into a string. We subsequently reverse the string (the [::-1] slice). If both are same then the underlying number is a palindrome.
Find the greatest product of five consecutive digits in the 1000-digit number.
An interesting side note here is how does one specify a 1000 digit number. In this case I chose to use the python “”” string delimiter to specify a multiline string exactly as described in the problem statement. However such a string has embedded \n characters which need to be removed. (lines 1 to 22).
Lines 31 and 32 create a sequence of all the substrings of 5 consecutive digits in the above number. Lines 29 and 30 compute the product of all the numbers that form the substring. Line 28 represents the tuple of the substring followed by the product. The reduce function on lines 24 to 26 (which gets intialised with the initial data on line 33) then continuously selects the tuple with the maximum product. The final ‘’ indexing operation on line 33 then just selects the product from the number product tuple selected by the earlier reduce function.