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

^{5}, is also a fifth power. Similarly, the 9-digit number, 134217728=8^{9}, 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))

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

pn_list=[]

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

pn_list.append(pn(n))

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

print(‘Done’)

## 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: 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, thenfactors=(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 tofactorslist.

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

a=3

while a <= num:

if num%a==0:

return int(a)

break

a = a + 1

# This is the number from ProjectEuler

num = 600851475143

temp_num = num

facto_back =0

p_factors =[]

the_num=1

while temp_num > 1 :

if temp_num%2==0 :

p_factors.append(2)

temp_num = temp_num / 2

else:

facto_back =factors_of_n(temp_num)

p_factors.append(facto_back)

temp_num = temp_num / facto_back

print(p_factors)

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