Posts

Largest palindrome product

Problem:  A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Solution: list = [] for i in range ( 100 , 999 ): for j in range ( 100 , 999 ): num = i * j if str (num) == str (num)[:: - 1 ]: list . append(num) print ( max ( list )) Note: This problem was taken from Project Euler (www.projecteuler.net).

Largest prime factor

Problem: What is the largest prime factor of the number 600851475143?  Solution: 1 2 3 4 5 6 7 8 9 10 11 n = 600851475143 x = 2 while x * x <= n: while n % x == 0 : if n == x: break n = n / x x = x + 1 print (n) Note: This problem was taken from Project Euler (www.projecteuler.net).

Even Fibonacci numbers

Problem: 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, ... . By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Solution: x = 1 y = 1 z = 0 sum = 0 while z < 4000000 : z = (x + y) if z % 2 == 0 : sum = sum + z x = y y = z print ( sum ) Note: This problem was taken from Project Euler (www.projecteuler.net).

Multiples of 3 and 5

Problem: Find the sum of all the multiples of 3 or 5 below 1000. Solution: 1 2 3 4 5 sum = 0 for i in range ( 1000 ): if (i % 3 == 0 or i % 5 == 0 ): sum = sum + i print ( sum ) Note: This problem was taken from Project Euler (www.projecteuler.net). 

Simple Addition

Problem: Find the sum of two integers num1  and num2 . Python solution: 1 2 3 num1 = int ( input ()) num2 = int ( input ()) print (num1 + num2)