## 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

## 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”.wikipedia.org

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

# SOLVED

# Completed on Fri, 5 Jul 2019, 08:53

palin =0

def palindromic(n) :

n_list=[]

for each in str(n) :

n_list.append(each)

n_last = len(n_list)-1

n_first =0

x=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))

## 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.

Example:

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_11s={“11″:”eleven”,”12″:”twelve”,”13″:”thirteen”,”14″:”fourteen”,”15″:”fifteen”,”16″:”sixteen”,”17″:”seventeen”,”18″:”eighteen”,”19″:”nineteen”}

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

num_100=’hundred’

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 :

d0=str(d0)+str(0)

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

def num_3_ds (num):

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

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

## 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

break

return result

p_count=0

num =0

while p_count <=10001:

num +=1

if is_prime (num):

p_count +=1

print(p_count,num)

print(num,p_count-1)

## 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():

x=1

fibo=[1,1]

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

fibo.append(fibo[-2]+fibo[-1])

x +=1

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

# Call the function

get_fibonacci()

## Python: Even Fibonacci Numbers

**Python: Even Fibonacci Numbers **

** ProjectEuler Problem No.2 **

Easy fast task in ProjectEuler, **The Task is** to find the sum of the even-valued terms in Fibonacci sequence whose values do not exceed four million.

In this code we will add some print-statment to just show how mane even numbers there and the summation of it.

**The Code:**

# Even Fibonacci Numbers

# projectEuler Problem No. 2

def get_fibonacci_sequence():

n1 = 1

n2 = 1

fibo = 1

while fibo < 4000000:

fibo = n1+n2

n1 = n2

n2 = fibo

if fibo% 2 == 0:

even_fibo_num.append(fibo)

tot = 0

even_fibo_num = []

get_fibonacci_sequence()

print(‘\n We fond {} even fibonacci”s number less than 4000000.’.format(len(even_fibo_num)))

for each in even_fibo_num:

tot = tot + each

print(‘ The summation on those even Fibonacci”s is: ‘, tot)

## Python: Square Digit Chain

**Python: Square Digit Chain **

** ProjectEuler Problem No.92**

Here we have a mathematical definition called Happy Number..

A Happy Numberis 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.

(Wikipedia).

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

the_chain=[]

def get_square_digit_chain(n):

tot=0

the_chain.append(n)

while n != 0:

tot=0

for each in str(n):

tot= tot + int(each)**2

n = tot

if n in the_chain:

return n

else:

the_chain.append(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’)

else:

if chain_closed == num:

print(“We have a Perfect Chain”)

print(the_chain,’Closed on’,chain_closed)

else:

print(“We have a Tail Chain”)

print(the_chain,’Closed on’,chain_closed)