## Python: Multiples of Numbers

**Python: Multiples of 3 and 5 **

** Problem No.1 in ProjectEuler **

**This** is very easy, very short task to work on, the task as is in **ProjectEuler **like this “Find the sum of all the multiples of 3 or 5 below 1000.”

**My way,** as i like to do open code works for any numbers, we will ask the user to enter three numbers, num1 and num2 will be as (3 and 5) in the task, my_range will be as the 1000. So the code can get the sum Multiples of any two numbers in a ranges from 1 to my_range.

**The Code:**

# Multiples of 3 and 5

# ProjectEuler: Problem 1

def Multiples_of_N (num1,num2,my_range):

tot=0

for t in range (1,my_range):

if t %num1==0 or t%num2 ==0 :

tot = tot + t

return tot

print ‘\nDescription: This function will take three variables, two numbers represint the what we want to get there Multiples, then we ask for a range so we will start from 1 to your range.\n’

num1=int(input(‘Enter the first number:’))

num2=int(input(‘Enter the second number:’))

my_range =int(input(‘Enter the range (1, ??):’))

total=Multiples_of_N (num1,num2,my_range)

print ‘\nYou entered ‘,num1,’,’, num2,’ So the sum of all multiples of those number in range (1-‘,my_range,’) = ‘,total

## Python: Largest product in series

**Python: Largest product in a series **

** Problem 8 @ projectEuler **

In Problem 8, ProjectEuler wants to find the thirteen adjacent digits in the 1000-digit number that have the greatest product.

In this task i use a for loop to check each 13-dig set, each time creating a set of 13 digits starting from (0,13) then (1,14)..(2,15)….. and so-on. for each set i get the product of its digits and store it in an a array of [set,total] each time if total of the new set is larger than what we have in the array[total] then we write the new values to the array, we call the array largest.

**The Code:**

# Largest product in a series

# ProjectEuler: Problem 8

num=’7316717653133062491922511967442657474235534919493496983520312774506326239578318016984

8018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715

6932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308

13533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271

218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224

3525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197

8468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345

0658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947

6545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096

2455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588

6116467109405077541002256983155200055935729725716362695618826704282524836008232575304207529

63450

‘

v1=0

v2=13

set1=num[v1:v2]

largest =[0,0]

tot=1

for x in range((1000)):

set1=num[v1:v2]

for each in set1:

tot=tot * int(each)

if tot > largest [1]:

largest[0] = set1

largest[1] = tot

tot=1

tot =1

v1=v1+1

v2=v2+1

print’The thirteen adjacent digits are’,largest[1],’there product is ‘,largest[0]

## Python: Factorial Digit Sum

**Python: Factorial Digit Sum **

** Problem 20 @ projectEuler**

**The Task:** The task in projectEuler P20 is to get the sum of the digits in the number Factorial of 100!

**Factorial Ndefinition**The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800.

**Problem 20** is another easy problem in projectEuler, and we will write two functions to solve it. __First one__ is a Factorial_digit_sum this one will return the factorial of a number. __The second__ function will calculate the sum of all digits in a number N and we will call it sum_of_digits.

**Clarification** As long as i just start solving or posting my answers to projectEuler portal, i am selecting problems and not going through them in sequence, that’s way my posts are jumps between problems, so if i am posting the code to solve problem 144 *(for example)* that does’t meaning that i solve all problems before it.

print of solved screen:

**The Code:**

#Python: Factorial Digit Sum

#Problem No.20 on projectEuler

def Factorial_digit_sum(num):

if (num == 0) :

return 1

else:

return num * Factorial_digit_sum(num-1)

num=100

fact =Factorial_digit_sum(100)

print fact,’is the Factorial of {}.’.format(num)

def sum_of_digits(dig):

t = 0

for each in dig:

t = t + int(each)

print ‘\nThe sum of your number is’,t

sum_of_digits(str(tot1))

## Python: Collatz Sequence

**Python: Longest Collatz Sequence **

**Problem No.14 in ProjectEuler**

**Definition** Wikipedia: Start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

So the formula is: Starting with n, the next n will be:

n/2 (if n is even)

3n + 1 (if n is odd)

If we start with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1.

**A walk through: **

n=13, 13 is odd, then n = 13 * 3 + 1 , n= 40

n=40, 40 is even, then n= 40/2, n=20

n=20, 20 is even, then n=20/2, n=10

n=10, 10 is even, then n=10/2, n=5

n=5, 5 is odd, then n=5*3+1 , n=16

n=16, 16 is even, then n=16/2, n=8

n=8, 8 is even, then n=8/2, n=4

n=4, 4 is even, then n=4/2, n=2

n=2, 2 is even, then n=2/2, n=1

n=1 then end of sequence

**The Task:** The task in ProjectEuler is to searching for a the Number N, under one million, that produces the longest chain.

Overview to my python cases:In my company, we are not allowed to download any software, so i don’t have any Python platform. To solve this i am using an online python interpreter, some time it’s become slow. So in this code (and others) i am spiting the range in 10 each with 100,000 then running the code to get the longest chain in each range. So the Number N, under one million, that produces the longest chain is:

**The Answer: **In my previous codes or math solving challenges in pybites or ProjectEuler I am solving the problems, writing the code, but not posting my answer to ProjectEuler platform. Today, and with Problem No.14 i decide to post the answer in the ProjectEuler platform for the first time just to see what will happen. The answer was 837799, and I get this page.

*In the code bellow, i set the range from 1 to 50000.*

**The Code:**

chain2=[]

longest=[0,0]

def collatz_Seq(num):

t= num

chain=[num]

while t !=1 :

if t%2==0:

t=t/2

chain.append(int(t))

else:

t=3*t+1

chain.append(int(t))

return chain

for num in range (1,50000):

chain2 = collatz_Seq(num)

if len(chain2) > longest[0]:

longest[0] = len(chain2)

longest[1] = num

chain2=[]

print(‘num:’,longest[1],’ has a longest chain: ‘,longest[0])

## Python: The Factors

**Python: Factors of the Number N**

This is a short task to get the factors of a given number. The Definition of Factors of N is: The pairs of numbers you multiply to get the N number.

For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4

In this task we will write a Python code to ask the user for a number N then will get all the pairs number that if we multiply them will get that N number, we will store the pairs in a array ‘factors’.

**The Code:**

def factors_of_n(num):

a=1

factors=[]

while a <= num:

if num%a==0:

if (num/a,a) not in factors:

factors.append((a,int(num/a)))

a = a + 1

return factors

#Ask the user for a number

num=int(input(“Enter a number: “))

print(factors_of_n(num))

## Python: Amicable Numbers

**Python: Amicable Numbers **

** Problem No.21 on projectEuler **

In this task we need to calculate the SUM of all divisors of N, from 1 to N.

Then we calculate the Sum of (sum of N divisors ) let’s say M .

Now if

**For example**, the proper divisors of **A=220 **are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore F(220) = 284. And the proper divisors of **B=284 **are 1, 2, 4, 71 and 142; so F(284) = 220. So F(A) =B and F(B)=A then A and B are amicable.

Definition:If f(a) = b and f(b) = a, where a ≠ b,

thena and b are anamicable pairand each of a and b are calledamicable numbers.

**In this task** we will ask the user to enter a Range of numbers and we will search for all Amicable Numbers pairs in that range(from – to) if we fond one we will print out the number and the divisors list. The main function here is the one that get the sum of divisors, we will call it get_divisors_sum and we will examine Amicable with If statement.

Hint ..

To check if the sum of divisors from both said are equal we use (t2==x )

AND that this sum are not same we use (t1!=t2)

AND that we did not print the pair before we use: (t2 not in ami_pair)

**The Code:**

#Python: Amicable Numbers

#Problem No.21 on projectEuler

num1=int(input(‘The range Start from:’))

num2=int(input(‘The range Ends at:’))

t1=0

t2=0

l=[]

l1=[]

l2=[]

ami_pair=[]

def get_divisors_sum (num):

t=0

l=[]

for a in range (1,num):

if num%a==0 :

l.append(a)

t=t+a

return t,l

for x in range(num1,num2):

l1=[]

t1,l1=get_divisors_sum(x)

l2=[]

t2,l2=get_divisors_sum(t1)

if (t2==x) and (t1!=t2) and (t2 not in ami_pair):

print(‘\nget_divisors_sum({}) is {} divisors={}’.format(x,t1,l1))

print(‘\nget_divisors_sum({}) is {} divisors={}’.format(t1,t2,l2))

print(‘\nSo {} and {} are Amicable Numbers .’.format(t1,t2))

ami_pair.extend((t1,t2))

## Python Project: Sum of power of digits

**Python: Sum of power of digits **

** Problem No.16 **

In Problem No.16, projectEuler ask to find the Sum of power of digits in the number, for example if we have 2^15 (2 to power of 15) the answer is 23768 then we need to calculate the sum of this number (2+3+7+6+8 ) that’s equal to 26.

**The Task:** So our task in this project is to find the sum of the digits of (2^1000). To write this as a program and to make it more __general__ we will ask the user to input the number and the power he want, then I start thinking to restrict user from input large numbers that could cause CPU problems, but then i decide to keep it open as is.

**The Code:**

num=2

p=15

num=int(input(“Enter a number “))

p=int(input(“Enter a power “))

a=num**p

def sum_of_digits(num,p):

a=num**p

l=[int(i) for i in str(a)]

print(l)

for x in range (len(l)):

t=t+l[x]

print (“sum =”,t)

## Python: Prime Numbers in Range

**Python: Prime Numbers in a Range**

Our task here is simple as the title, we will have a range and will test each number to see if it is a prime then we will add it to a list, once we finish we will print out the list.

To complete this task we will use one of our function we create last time (**Read:** is prime post).

So, here we will ask the user to input two numbers num1 and num2 the we will pass all the numbers in the range to ** is_prime()** and store the result in a list.

**The Code:**

#Function to get all Prime numbers in a range.

#Ask the user to enter two numbers

print ‘Get all Prime numbers in a range\n’

num1=int(input(“Enter the first number in the range: “))

num2=int(input(“Enter the last number in the range: “))

#create the list

prime_list=[]

def get_all_prime(num1,num2):

for x in range (num1,num2):

if is_prime(x) ==’Prime’:

prime_list.append(x)

return prime_list

#Call the function and print the list

get_all_prime(num1,num2)

print prime_list

print len(prime_list)

## Python Project: Filter Numbers with a List

**Python: Bite 107 **

** Filter numbers with a list comprehension**

I was happy to join **Pybites ** and start to solve the code-challenges, my problem from the first bite was that i am solving the challenges in other online python interpreter, and it looks working fine and the output seams as it should be, but once I copy the code in Pybite portal and doing the code test it fail and gives errors.

In this challenge **Bite 107**, after a list of numbers the task is to return the positive even number. Now in the task description it mentioned to use (__elegant list comprehension; one line of code__). I start to work on it, immediately it jump to my head a for loop and if each item is even and more than 0 add it to my_list, then return my_list. After coding, the output was a list with positive and even numbers in a Number-list, but it failed in the Bite test portal. WAY!.

This type of failure was against me in other challenges also, and i was just setting all the day thinking what happen!. With **Bite 107** I decide to click on “**Show Solution**” button just to see what is wrong.

**One line code:**The Solution was in one line of list brackets after a return statement congaing for and if without my_list. just like that. I start smiling, and ask my self “**How could i know that i can write it like this??”**. Yes it is beautiful, Yes elegant and yes one line of code, and yes my-way of coding gives the answer that we want, my code pass my testing exam.

**Concloguen**: I think i come up with a good resource of learning python, I don’t want to read book’s but reading codes that doing things and having the alternatives of writing same code; this will help me a lot. I am not coding for NASA or in other sensitive area that processing time is essential and micro-milli seconds make difference, so one line of four-line to-me is OK. 🙂

**Acknowledge**: I’d like to thank **Pybite **community for there contribution in spreading knowledge and codes, and for this challenges. I will read the challenges, and try my best to solve them as elegant as i can, also i will not stop in one point because my code is 2 lines more than what should be.

**Last thing**, Bite 107 here is my code and the elegant code. Be calm and positive.

**The Code:**

**#First: My Code**

def filter_positive_even_numbers(numbers):

my_list=[]

for i in range(len(numbers)):

if (i>0 and i%2==0) :my_list.append(i)

return my_list

print (filter_positive_even_numbers(numbers))

**#The optimal solution **

def filter_positive_even_numbers(numbers):

return[n for n in numbers if n > 0 and n % 2 == 0]

## Python Project: Sum of the Square and Square of the Sum

**Python: Sum of the Square and Square of the Sum **

**Difference of sum of the square and the square of the sum**

I fond this on projecteuler.net Usually I add some steps to there problems to make it more application look and feel. Later on, we’ll see how.

**Problem assumption**: If we said that we have a range of numbers (1,10) then the sum of this range is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55, Now the Square of the sum is 55^2; thats mean the Square of the sum of (55) = 3025.

And for the same range, the Sum of Square means that we will get the Square of each number in the range then will get there summation. So with our range (1,10) Sum of Square is 1^2 + 2^2 + 3^2 + 4^2 + 5^2 …. + 10^2 = 385

The Problem #6 in ProjectEulerThe sum of the squares of the first ten natural numbers is,1

^{2}+ 2^{2}+ … + 10^{2}= 385The square of the sum of the first ten natural numbers is,(1 + 2 + … + 10)

^{2}= 55^{2}= 3025

Hencethedifference betweenthe sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

The Task:Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

**Inhancment** Now to make this task workking as application and to get more general output of it, first we will ask the user to input a range of the numbers, then we will applay the function on that range.

**The Code:**

# Problem 6 in ProjectEuler.

def square_of_sum(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+x

print(‘The Square of the Sun’,tot*tot)

return tot*tot

def sum_of_square(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+(x*x)

print(‘The Sum of the Square ‘, tot)

return tot

#Ask the user for his input.

num1 = int(input(‘Enter First number in the range: ‘))

num2 = int(input(‘Enter the last number in the range: ‘))

#Call the functions.

w1 = square_of_sum(num1, num2)

w2 = sum_of_square(num1, num2)

#Output the Difference.

print(‘The Difference is: ‘, w1-w2)