Posts Tagged ‘Qatar’

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: Champernowne’s constant

Python: Champernowne’s constant
Problem No.40 @ ProjectEuler
Completed on: Mon, 1 Jul 2019, 18:01

In This task No.40, basically we need to get some digits from a large decimal fraction, then finding the multiplication of those digits.

ProjectEuler assume that the fraction is: 0.123456789101112131415161718192021222324 …. until 1000000, then we should fined the digits in positions 1, and 10, 100, 1000, 10000, 100000 and 1000000. Here is a copy of the problem screen

So to solve this I create a string variable n_list then using for loop i store the numbers from 1 to 1000000 in it as [12345678910111213141516 … 1000000], and simply get the digits I want using the list index, and Finally I calculate the needed multiplication as required. .. And we solve it. ..

Follow me on Twitter..

Python: Combinatoric Selections

Python: Combinatoric Selections
Problem 53 @ projecteuler
Completed on: Mon, 1 Jul 2019, 11:32

This is one of the tasks that based on equations. To solve this, first let me Simplify it.

Simplifying the task:

if 1<= n <= 100 , and r <= n

using the equation of C(n,r) is

c(n,r) = n! / (r! *(n-r)!

How many values of c(n,r) greater than one-million

Since we need the factorial of n (n!) and (r!) then I will re-use our function get_factorial(num), and I will write another function to calculate the C(n,r) as in the equation above, I will call it combi(n,r), also I will use a while loop, and we need to set some variables to help us in the execution. In this part, I will use a list called values_over_1m to store all the values in it, although this is Not required in the Problem 53, but i will keep it so later we can print them all to the screen or just get the total values of it. And so we did .. and we get the right answer.

The Code:

# Combinatoric selections
# Problem 53 @ projecteuler
# Completed on Mon, 1 Jul 2019, 11:32

if 1<= n <= 100 , and r <= n

using the equation of C(n,r) is
c(n,r) = n! / (r! *(n-r)!

How many values of c(n,r) greater than one-million


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

if (num == 0) :

return 1


return num * get_factorial(num-1)

def combi(n,r):

fn = get_factorial(n)

fr = get_factorial(r)

fnr = get_factorial(n-r)

return fn / (fr*fnr)

# To store the values more than 1 million.
values_over_1m =[]

nn = 1
rr = 1

start_value= combi(nn,rr)
stop_me = False

while stop_me == False :

nn +=1

rr =2

while rr < = nn :

start_value = combi(nn,rr)

if start_value >1000000 :


rr +=1

if nn > 99 :

stop_me = True

print(‘Ther is ‘,len(values_over_1m),’ Values meeting the equation.’)

Follow me on Twitter..

Python: Permuted Multiples

Python: Permuted Multiples
Problem No.52 @ Projecteuler

This task Completed on Mon, 1 Jul 2019, 06:08, the goal is to find a number X that if we get the X, X*2, X*3, X*4, X*5, X*6 all the numbers are same digits but in a different order.

I use a function called if_x_in_y (n1,n2) will return True if the two numbers has same digits, False if not. Then a while found_one is not True a loop will examine all numbers starting from 2 until we found_one.

The Code:

# Permuted multiples
# Problem No.52
# Completed on Mon, 1 Jul 2019, 06:08am (GMT+3)

# if x, x*2, x*3, x*4, x*5, x*6 has the same digits.

def if_x_in_y (n1,n2) :

for x in str(n1) :

if x not in str(n2) :

return False

for x in str(n2):

if x not in str(n1):

return False

return True

x_num = 2
found_one= False

while not found_one:

if if_x_in_y(x_num,x_num*2):

if if_x_in_y(x_num,x_num*3):

if if_x_in_y(x_num,x_num*4):

if if_x_in_y(x_num,x_num*5):

if if_x_in_y(x_num,x_num*6):

print(‘\n Yes, we found one ..’)

found_one = True

print(‘ The Number is ‘,x_num)

x_num += 1


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: Square Digit Chain

Python: Square Digit Chain
ProjectEuler Problem No.92

Here we have a mathematical definition called Happy Number..

A Happy Number is defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either:
equals 1 (where it will stay), or
it loops endlessly in a cycle that does not include 1.

In ProjectEuler the task stated in this problem as ” How many starting numbers below ten million will arrive at 89?”

Enhancement: Here we will do something else, we will try to solve the task and post the answer to the projecteuler portal, BUT we are not talking about this here, we will use the concept of this task to generate chains of looped number and I will use it later in another post (project) and trying to represent this chains in a graphic way.

So to do this we need two functions, First one will read a number, get its digits, squaring each digit and get the summation. To keep our eyes on the numbers we need to store it, so we will use list called the_chain.

To check if we have reach a closed chain then we need to ask if the new number (sum of square digit) exists in the chain list or not. If exists we finish and will return the chain for more manipulating.

I will solve this on my way .. 🙂

In this code we will do the following:

1. We will ask the user to enter a number.

2. We will run the function on that number.

3. Outputs:

If we ends with 1 then we have a Happy Number.

If we have closed chain (current number exists in the chain) then we will have tow cases:

If the current number is the same as the start number, then we will call this “Perfect Chain“. Otherwise we will call it “Tail Chain

The Code:

# Square digit chain.
# Pprojecteuler problem No 92

num = 1

def get_square_digit_chain(n):



while n != 0:


for each in str(n):

tot= tot + int(each)**2

n = tot

if n in the_chain:

return n



#We ask the user to enter a number.
num =int(input(“Enter a number “))

chain_closed = get_square_digit_chain(num)
if chain_closed == 1:

print(“We have a Happy Number”)

print(the_chain,’This is Open Chain’)

if chain_closed == num:

print(“We have a Perfect Chain”)

print(the_chain,’Closed on’,chain_closed)


print(“We have a Tail Chain”)

print(the_chain,’Closed on’,chain_closed)

Follow me on Twitter..

Python : Curious Number

Python: Curious Number
Problem No.34 in ProjectEuler

Definition: A number is Curious Number if the factorial of their digits equal to the number itself.

Example: 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Our Task: We will write two functions, first one will get (return) all digits in the number, then another function to get the factorial of each digits in that number then with If statement we will examine the result.
Enhancement: We will ask the user to enter a number and we will check if it is a Curious Number.
We will reuse some of our functions that we wrote in previous posts.

The Code:

print(‘\nEnter a number to see if it is a Curious Number or not.’)
num=input (‘\nEnter a number: ‘)
num=input (‘Enter a number: ‘)
# To get the digits In a number
def digits_in_num (num):

for each in str(num):


# To get the Factorial of a number
def Factorial_digit_sum(num):

if (num == 0) :

return 1


return num * Factorial_digit_sum(num-1)

for each in digs:

print(‘factorial :’,each,’ is ‘,Factorial_digit_sum(int(each)))

tot = tot + Factorial_digit_sum(int(each))

print(‘\nTotal sum of the Factorial of each digits is: ‘,tot)
if int(num) == tot:

print(num ,’Is a Curious Number.’)

print(num ,’Is NOT Curious Number.’)

Follow me on Twitter..