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

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

**Python: Non-abundant sums**

**Problem No.23 @ ProjectEuler**

In projecteuler Problem No. 23 talking about **Non-abundant sums** to clear this it state three categories for a numbers.

1. A number N called **Perfect Number** if the sum of its divisors are equal to the number it self. So 28 is a perfect number because the divisors of 28 are 1,2,4,7,14 they equal to 28. __( 1 + 2 + 4 + 7 + 1 4 = 28)__

2. A number N is called **Deficient** if the sum of its proper divisors is less than the number it self N.

3. And a number N is called **Abundant** if this sum of its proper divisors is exceeds N (number it self)

**Enhancement:** So instead of solving problem No.23, we will write a code to determent the group that a number belongs to. As we trying to make a general cods, we will ask the user to enter a number then we will call the function to collect all the divisors of N and get its sum and gives it a name according to the classification we just talk about.

**The Code:**

# Python: Non-abundant sums

# Problem No.23 @ ProjectEuler

# We solve it in my way

num_divisors=[]

def get_num_group (num):

for x in range(1,num):

if num%x == 0:

num_divisors.append(x)

num =int(input(‘Enter a number: ‘))

div_sum =0

get_num_group(num)

for each in num_divisors:

div_sum = div_sum + each

print(‘\n Divisors of {} are,’.format(num),num_divisors)

print(‘ The sum of the Divisors is ‘,div_sum)

if div_sum == num :

print (‘ The number {} is Perfect Number’.format(num))

elif div_sum < num :

print (‘ The number {} is Deficient ‘.format(num))

else:

print (‘ The number {} is Abundant ‘.format(num))

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