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

## Python Project

**Python: Split and join **

** Split up the message on newline **

The target of this task is to Split a given text message on the newline (\n) then use the join builtin to stitch it together using a ‘|’ (pipe).

I fond this on the pybites web-site as a code-challenges bites #104. In our case we will print the new Message within the function but we can return it back as a variable for more usage in our code..

**Enhancements**: If we want to do some enhancements to this code:

1. We can read the message from a file.

2. Letting the user to select the joining character.

3. Also we may export the final message to another txt file.

We assume our message is “Hello world!\nWe hope that you are learning a lot of Python.\nHave fun with our Bites of Py.\nKeep calm and code in Python!\nBecome a PyBites ninja!”

**The Code:**

def split_in_columns(message=message):

sp_message = message.split(“\n”)

message=”|”.join(sp_message)

print(message)

split_in_columns(message=message)

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

numbers=[]

# Function to get the Factorials of a number.

def get_factorial(num):

if (num == 0) :

return 1

else:

return num * get_factorial(num-1)

def get_summation(the_list):

tot=0

for each in the_list:

tot +=each

return tot

start_num = 2

the_num = start_num

keep_chain=[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

keep_chain=[start_num]

while the_facto_d_sum != start_num:

for each in str(the_num):

numbers.append(get_factorial(int(each)))

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

else:

keep_chain.append(the_num)

numbers=[]

if len(keep_chain) == 60:

chain_count +=1

keep_chain =[]

start_num +=1

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

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

sequence_list=[]

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.

sequence_list.append(a**b)

for a in range (2,101):

get_sequence (a) # Calling the function

# Get the total elements in the sequence_list

print (len(sequence_list))