Posts Tagged ‘ProjectEuler’

Python: Smallest Multiple

Python: Smallest multiple
Problem 5 @ projecteuler
Completed on: Thu, 4 Jul 2019, 22:30

Here I am quoting form ProjectEuler site:”

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 positive number that is evenly divisible by all of the numbers from 1 to 20?”

So to solve this simple task all we need to loop through numbers and divide it by a list of (1,20) if yes return True otherwise return False and got to another number.
and so we done..

The Code:

codes here

Follow me on Twitter..

Python: Powerful Digit Counts

Python: Powerful Digit Counts
Problem No.63 @ ProjectEuler
Completed on: Completed on Thu, 11 Jul 2019, 17:21

Just to make my post simple, i am quoting from ProjectEuler page

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.
How many n-digit positive integers exist which are also an nth power?

Then, we need to find the loop that will solve this, and we did..

The Code:

# P63
# Power digit count
# Solved
# Completed on Thu, 11 Jul 2019, 17:21

c = 0
for x in range (1,50):

for p in range (1,50) :

if (len(str(x**p)) == p ):

c += 1

print(‘\n We have {} n-digit integers exist which are also an nth power.’.format(c))

Follow me on Twitter..

Python: Pentagon Numbers

Python: Pentagon Numbers
Problem No.44 on ProjectEuler
Completed on: Thu, 11 Jul 2019, 18:37

This problem talking about the Pentagonal numbers and gives us a formula. Using that formula for a certain range of numbers, the generated sequence showing that P4 + P7 = 22 + 70 = 92, 92 is the P8, but if we subtracting (P7 – P4) = 70 – 22 = 48, 48 is not in the generated sequence of pentagonal numbers, so 48 is not pentagonal.

The task here is to find the pair of pentagonal Pj,Pk which their sum and difference are Pentagonal D = Pk – Pj is minimised.(we need to get the D).

The Code:

# P44
# Pentagon Numbers
# Solved
#Completed on Thu, 11 Jul 2019, 18:37

def pn(n):

return int(n*(3*n-1)/2)


for n in range (1000,3000) : # I start increasing the range step by step.


we_found_it = False
for x in range (0,len(pn_list)-1) :

px= pn_list[x]

for y in range (x+1,len(pn_list)-1) :

py= pn_list[y]

if (px+py) in pn_list:

if (py-px) in pn_list:

print(‘\n We found one ‘,px,py,’D = ‘,py-px )

we_found_it = True

if we_found_it : break


Follow me on Twitter..

Python: Largest Palindrome Product

Python: Largest Palindrome Product
Problem No.4 @ Projecteuler
Complete on: on Fri, 5 Jul 2019, 08:53

The task was to find the largest palindromic number that been generated from multiplying two of 3 digits number.

Definition: Palindromic numbers are numbers that remains the same when its digits are reversed. Like 16461, we may say they are “symmetrical”

To solve this I first wrote a function to check if we can read a number from both side or not, Then using while and for loop through numbers 100 to 999, and store largest palindromic, we select the range (100,999) because the task is about tow number each with 3 digits.

The Code:

# Problem 4
# Largest palindrome product
# Completed on Fri, 5 Jul 2019, 08:53

palin =0
def palindromic(n) :


for each in str(n) :


n_last = len(n_list)-1

n_first =0


while (n_first+x != n_last-x) :

if n_list[n_first+x] != n_list[n_last-x] :

return False

else :

x +=1

if (n_first +x > n_last -x):

return True

return True

for set1 in range (1,999):

for set2 in range (set1,999):

if palindromic(set1 * set2) :

if (set1 * set2) > palin :

palin =(set1*set2)

print(‘\n We found it:’,palin, ‘coming from {} * {}’.format(set1,set2))

Follow me on Twitter..

Python: Number Letter Counts

Python: Number Letter Counts
Problem 17, Projecteuler

For problem 17, projectEuler ask for the total numbers of alphabetics in all words of numbers from 1 to 1000.

Here I am coping from there page.

Once I read the task, I decide to create my own version of the requirement. I start to assume that I may have a dictionary with some numbers and words, then base on what ever the user will input (number between 1 and 999) then I shall convert it to words.

1 –> one
8 –> eight
234 –> tow hundred thirty-four .. and so on.

I start searching the net to get the words I need, and store them in 3 dictionaries.

num_1_9 = {“1″:”one”,”2″:”two” .. ext.
num_10s = {“10″:”ten”,”20″:”twenty” .. ext.
num_11s = {“11″:”eleven”,”12″:”twelve”.. ext

Then, I start writing the functions for each group of numbers/dictionaries, so if the user enter a number we will read number of digits if it is 1 then we call def num_1_d(), if it is 2 digits we call def num_2_ds() and if it is 3 digits we call num_3_ds(). At the end, we got the right answer for Projecteuler, and here is the code in my way, I am asking the user to enter a number then i convert it to a corresponding words.

The Code:

# Date:27/6/2019
# ProjectEuler
# Problem No.17
# Completed on Sun, 30 Jun 2019, 10:30

num_1_9 = {“1″:”one”,”2″:”two”,”3″:”three”,”4″:”four”,”5″:”five”,”6″:”six”,”7″:”seven”,”8″:”eight”,”9″:”nine”}


num_10s ={“10″:”ten”,”20″:”twenty”,”30″:”thirty”,”40″:”forty”,”50″:”fifty”,”60″:”sixty”,”70″:”seventy”,”80″:”eighty”,”90″:”ninety”}


def num_1_d(num):

if len(str(num)) == 1:

return num_1_9[str(num)]

def num_2_ds(num):

d0,d1 = num[0], num[1]

if int(num[1])== 0 :

return num_10s[str(num)]

elif int(num[0]) == 1 :

return num_11s[str(num)]

elif (int(num[0])) >1 :


return ‘{}-{}’.format(num_10s[str(d0)],num_1_9[str(d1)])

def num_3_ds (num):


if (int(num[1])==0) and (int(num[2])==0) :

return ‘{} {}’.format(num_1_9[str(d0)],num_100)

elif (int(num[1])>0):

d1 = str(d1)+str(d2)

return ‘{} {} and {}’.format(num_1_9[str(d0)],num_100,num_2_ds(d1))

elif (int(num[1])==0) and (int(num[2])>0):

d1 = str(d1)+str(d2)

return ‘{} {} and {}’.format(num_1_9[str(d0)],num_100,num_1_9[str(d2)])

num =0

while num !=’f’ :

num =input(‘\nEnter a number: (1-999)’)

if len(str(num)) == 1:

print(num ,’ is ‘,(num_1_d(num)))

elif len(str(num)) == 2:

print (num ,’ is ‘,(num_2_ds(num)))

elif len(str(num)) == 3:

print (num ,’ is ‘,(num_3_ds(num)))

This is Output screen for my-way version

Follow me on Twitter..

Python: 10001st Prime

Python: 10001st Prime
Problem No.7 @ ProjectEuler

This is one of shot projects you may find on ProjectEuler, the task is find the prime No 10001, I just run my is_prime function, if the number is prime will add 1 to the counter, until we reach 10001, that will be the answer.

The Code:

# 10001st prime
# Problem 7
# Solved: Wed, 26 Jun 2019, 05:57am (GMT+3)

def is_prime(num):

result = True

for t in range (2,num):

if num%t==0 :

result= False


return result

num =0
while p_count <=10001:

num +=1

if is_prime (num):

p_count +=1


Follow me on Twitter..

Python: Largest Prime Factor

Python: Largest Prime Factor
Problem No.3 on ProjectEuler

Quite easy one to solve, in this task we need to get all the factors of a given number, then to print out the largest prime one.

To solve this with a Large Number I search the net and i fond an article explain it. So here i am putting it in points.

Factors of Large Numbers:
1. First ask if the number N is (Even or Odd)?
2. If the number N is Even, then factors=(2) then, the new N = N Divide by 2.
3. If the number N is Ood, then start form 3 and get/find smallest prime factors (SPF) of it.
4. Add the smallest prime factors to factors list.
5. Then, the new N = N Divide by smallest prime factors (SPF).
6. If the new N is Even goto line 2, If the new N is Odd goto line 3.

The Code:

# Largest prime factor
# Problem No.3
# Solved

def factors_of_n(num):


while a <= num:

if num%a==0:

return int(a)


a = a + 1

# This is the number from ProjectEuler
num = 600851475143
temp_num = num
facto_back =0
p_factors =[]

while temp_num > 1 :

if temp_num%2==0 :


temp_num = temp_num / 2


facto_back =factors_of_n(temp_num)


temp_num = temp_num / facto_back

print(‘\n Largest Prime Factor of {} is {}’.format(num,max(p_factors)))

Python: Digit Factorial Chains

Python: Digit factorial Chains
Problem No.74 @ ProjectEuler

In this task, we need to produce a chain of the summation of a factorial number and keep chasing each number in the chain until it starts repeat, we stop there and count the length of the chain, if its 60 then we increase a counter. ProjectEuler task 74 ask to do this and get all the numbers (How many are they?) below one Million (1000000) that has 60 numbers in it’s chain.

So we start to write two functions, one to get the factorial of a number, and the other to get the summation of the numbers in a list. Then with two (while) loop, an outer one to count the 60’s chain numbers, and an inner one to produce the chains.

At the end we manage to solve the problem 74 using Pythonanywhere.

The Code:

#Digit factorial chains
#Problem 74


# Function to get the Factorials of a number.
def get_factorial(num):

if (num == 0) :

return 1


return num * get_factorial(num-1)

def get_summation(the_list):


for each in the_list:

tot +=each

return tot

start_num = 2

the_num = start_num
chain_count = 0

print(‘We start with number ‘,start_num)
the_facto_d_sum = 0

while start_num < 1000000 :

the_num = start_num


while the_facto_d_sum != start_num:

for each in str(the_num):


the_facto_d_sum = get_summation(numbers)

the_num = the_facto_d_sum

if the_num in keep_chain :

the_facto_d_sum = start_num




if len(keep_chain) == 60:

chain_count +=1

keep_chain =[]

start_num +=1

print(‘we have {} numbers with 60 ‘.format(chain_count))

Follow me on Twitter..

Python: 1000-Digit Fibonacci

Python: 1000-Digit Fibonacci
Problem No.25, ProjectEuler

Another easy fast 5-minites task on projecteuler that Playing around Fibonacci Numbers, the task is to find the first Fibonacci index to contain 1000 digits.

So we will write a function to get the fibonacci numbers and each time we will check if it’s digits reach 1000, if not we will go for generating the next number.

The Code:

#1000-digit Fibonacci number
# problem no 25
# Solved.

def get_fibonacci():



while len(str(fibo[x])) != 1000:


x +=1

print(‘The index of the first term in the Fibonacci sequence to contain 1000 digits is ‘,x+1)

# Call the function

Follow me on Twitter..

Python: Distinct Powers

Python: Distinct Powers
ProjectEuler Problem No.29

In this project we have a sequance of numbers based on a powered number, it takes ten minites or less to write the code, test it and applay the needed figures to solve the problem. Thie code can be shorten, but I am using the classic way to write functions with comments on code.

Here is the Problem as on the ProjectEuler Portal:
Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

The Code:

#Distinct powers
#Problem 29


def get_sequence (a):

for b in range (2,101):

if a**b not in sequence_list:

#If the elements NOT exist in the list then add it.


for a in range (2,101):

get_sequence (a) # Calling the function

# Get the total elements in the sequence_list
print (len(sequence_list))

Follow me on Twitter..