Archive

Posts Tagged ‘Language’

Python: Digit fifth Powers



Python: Digit Fifth Powers
Projecteuler Problem No.30

This was an easy task and I solve it on my mobile during a brain resting session 😜. I will just copy the problem statement as it is in ProjectEuler ..



Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Read it on Projecteuler


My Problem When I start solving the task i was wondering how far i should check the numbers? We can’t just go for ever, we must stop in some range. I search the web for such cases an i fond a post that clearing this with a formula. I will explain this in my way.




Finding the Upper Limits:
1. We are talking about Power (P=5)
2. We are using the (Base ten) numbers, so the highest digit is 9. Then:
3. 9 power 5 (9p5 = 59049)
4. The digits in (59049) are D=5.
5. Finally, The Formula is (D * 9p5), 5 * 59049 = 295245
6. So, The Upper Limits = 295245



According to the “Finding the Upper Limits” section, if we want to use the power (4) then the upper limit will be:
9p4 = 6561
6561 is a 4 digits
upper limit = 4 * 6561 = 26244



The Code: [The code is for power 4]

# Digit Fifth Powers
# Projecteuler Problem 30

num = 2
pdig = []
wefound = []
thesum = 0

while num < 26244 :

for each in str(num):

pdig.append(int(each) ** 4)

for x in pdig:

thesum = thesum + int(x)

if thesum == num:

wefound.append(num)

print(‘\n Number =’, num)

print(‘ Digits Power 4 =’, pdig)

print(‘ The Sum ‘, thesum)

num = num + 1

pdig = []

thesum = 0

thesum = 0

for x in wefound:

thesum = thesum + x

print(“\n The Numbers that the 4th power of its each digit = itself are: “,wefound)
print(” The Sum of the numbers is: “,thesum)






Follow me on Twitter..



Advertisements

Python: Self Power



Python: Self Powers
Problem No.48 on ProjectEuler

Another easy task in Problem No.48. We have to find the power of each number to itsefl in the range of 1 to 1000 and get the sum of all numbers, then to find the last ten digits of the series.

In this Task we will save the powers in a set name powers then we run a for loop
to get the sum of all elements in the lest, later will reade the last ten digits.

Enhancment In this problem I will not do any more than solving the problem, but if we want to enhance the project, we can ask the user to enter a range and we perform the function on that range of numbers.




The Code:

powers=[]

def get_power(num):

powers.append(num**num)

for x in range(1,1001):

get_power(x)

sum=0
for each in powers:

sum=sum+each

print(powers)
print (‘the sum is ‘,sum)






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:


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

for each in str(num):

digs.append(each)

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

if (num == 0) :

return 1

else:

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.’)
else:

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









Follow me on Twitter..



Python: Multiples of Numbers

April 29, 2019 Leave a comment


Python: Multiples of 3 and 5
Problem No.1 in ProjectEuler

This is very easy, very short task to work on, the task as is in ProjectEuler like this “Find the sum of all the multiples of 3 or 5 below 1000.”

My way, as i like to do open code works for any numbers, we will ask the user to enter three numbers, num1 and num2 will be as (3 and 5) in the task, my_range will be as the 1000. So the code can get the sum Multiples of any two numbers in a ranges from 1 to my_range.

Check the answer if you like.













The Code:


# Multiples of 3 and 5
# ProjectEuler: Problem 1

def Multiples_of_N (num1,num2,my_range):

tot=0

for t in range (1,my_range):

if t %num1==0 or t%num2 ==0 :

tot = tot + t

return tot

print ‘\nDescription: This function will take three variables, two numbers represint the what we want to get there Multiples, then we ask for a range so we will start from 1 to your range.\n’
num1=int(input(‘Enter the first number:’))
num2=int(input(‘Enter the second number:’))
my_range =int(input(‘Enter the range (1, ??):’))

total=Multiples_of_N (num1,num2,my_range)

print ‘\nYou entered ‘,num1,’,’, num2,’ So the sum of all multiples of those number in range (1-‘,my_range,’) = ‘,total






Follow me on Twitter..



Python: Largest product in series

April 28, 2019 Leave a comment


Python: Largest product in a series
Problem 8 @ projectEuler

In Problem 8, ProjectEuler wants to find the thirteen adjacent digits in the 1000-digit number that have the greatest product.

In this task i use a for loop to check each 13-dig set, each time creating a set of 13 digits starting from (0,13) then (1,14)..(2,15)….. and so-on. for each set i get the product of its digits and store it in an a array of [set,total] each time if total of the new set is larger than what we have in the array[total] then we write the new values to the array, we call the array largest.



The Code:



# Largest product in a series
# ProjectEuler: Problem 8
num=’7316717653133062491922511967442657474235534919493496983520312774506326239578318016984
8018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715
6932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308
13533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271
218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224
3525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197
8468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345
0658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947
6545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096
2455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588
6116467109405077541002256983155200055935729725716362695618826704282524836008232575304207529
63450

v1=0
v2=13
set1=num[v1:v2]
largest =[0,0]
tot=1

for x in range((1000)):

set1=num[v1:v2]

for each in set1:

tot=tot * int(each)

if tot > largest [1]:

largest[0] = set1

largest[1] = tot

tot=1

tot =1

v1=v1+1

v2=v2+1

print’The thirteen adjacent digits are’,largest[1],’there product is ‘,largest[0]






Follow me on Twitter..



Python: Factorial Digit Sum

April 25, 2019 Leave a comment



Python: Factorial Digit Sum
Problem 20 @ projectEuler


The Task: The task in projectEuler P20 is to get the sum of the digits in the number Factorial of 100!

Factorial NdefinitionThe factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800.

Problem 20 is another easy problem in projectEuler, and we will write two functions to solve it. First one is a Factorial_digit_sum this one will return the factorial of a number. The second function will calculate the sum of all digits in a number N and we will call it sum_of_digits.

Clarification As long as i just start solving or posting my answers to projectEuler portal, i am selecting problems and not going through them in sequence, that’s way my posts are jumps between problems, so if i am posting the code to solve problem 144 (for example) that does’t meaning that i solve all problems before it.


print of solved screen:
















The Code:


#Python: Factorial Digit Sum
#Problem No.20 on projectEuler

def Factorial_digit_sum(num):

if (num == 0) :

return 1

else:

return num * Factorial_digit_sum(num-1)

num=100
fact =Factorial_digit_sum(100)
print fact,’is the Factorial of {}.’.format(num)

def sum_of_digits(dig):

t = 0

for each in dig:

t = t + int(each)

print ‘\nThe sum of your number is’,t

sum_of_digits(str(tot1))











Follow me on Twitter..



Python: Collatz Sequence

April 24, 2019 Leave a comment



Python: Longest Collatz Sequence


Problem No.14 in ProjectEuler

Definition Wikipedia: Start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

So the formula is: Starting with n, the next n will be:

n/2 (if n is even)

3n + 1 (if n is odd)


If we start with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1.

A walk through:

n=13, 13 is odd, then n = 13 * 3 + 1 , n= 40

n=40, 40 is even, then n= 40/2, n=20

n=20, 20 is even, then n=20/2, n=10

n=10, 10 is even, then n=10/2, n=5

n=5, 5 is odd, then n=5*3+1 , n=16

n=16, 16 is even, then n=16/2, n=8

n=8, 8 is even, then n=8/2, n=4

n=4, 4 is even, then n=4/2, n=2

n=2, 2 is even, then n=2/2, n=1
n=1 then end of sequence

The Task: The task in ProjectEuler is to searching for a the Number N, under one million, that produces the longest chain.


Overview to my python cases: In my company, we are not allowed to download any software, so i don’t have any Python platform. To solve this i am using an online python interpreter, some time it’s become slow. So in this code (and others) i am spiting the range in 10 each with 100,000 then running the code to get the longest chain in each range. So the Number N, under one million, that produces the longest chain is:


The Answer: In my previous codes or math solving challenges in pybites or ProjectEuler I am solving the problems, writing the code, but not posting my answer to ProjectEuler platform. Today, and with Problem No.14 i decide to post the answer in the ProjectEuler platform for the first time just to see what will happen. The answer was 837799, and I get this page.


















In the code bellow, i set the range from 1 to 50000.
The Code:


chain2=[]
longest=[0,0]
def collatz_Seq(num):

t= num

chain=[num]

while t !=1 :

if t%2==0:

t=t/2

chain.append(int(t))

else:

t=3*t+1

chain.append(int(t))

return chain

for num in range (1,50000):

chain2 = collatz_Seq(num)

if len(chain2) > longest[0]:

longest[0] = len(chain2)

longest[1] = num

chain2=[]

print(‘num:’,longest[1],’ has a longest chain: ‘,longest[0])









Follow me on Twitter..