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=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, 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))

Follow me on Twitter..


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)


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


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


Follow me on Twitter..

Python is_prime and time consuming

July 11, 2019 2 comments

Python: is_prime and time consuming
Function enhancement

Once i start solving projectEuler problems i notes that i need the prime numbers in most of cases, so I wrote a function called ‘is_prime’ and it works fine. Some time just to get all the primes in a given range takes some seconds, seconds in computer time means waiting a lot. With some other problems that we need to get the prime in large numbers my function looks slow, since I am not deep in math I search the net for a better way to get the primes or to check if a given number is prime or not.

Definition A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.
So as I understand a prime number is not dividable by any other numbers, so my is_prime function is taking a number let’s say n= 13, and start a loop from 2 to n=13 if we fond a number that divide 13 then 13 is not a prime.

Here is the code:

def is_prime1(num):

for t in range (2, num):

if num % t == 0 :

return False

return True

The function is working fine and we can get the prime numbers, but as I mention above, if we have a large number or a wide range, this will take some time. After searching the web, I found some facts regarding the Prime Numbers:

1. The only even prime number is 2. (So any other even numbers are not prime)
2. If the sum of a number digits is a multiple of 3, that number can be divided by 3.
3. No prime number greater than 5 ends in/with 5.

OK, now I can first cut any range to half by not going through even numbers (if even false). Then, I will see if the number end with 5 or not (if end with 5 false),last I will do a summation of the digits in the number if the sum divide by 3 (if yes false), and if the number pass then i will take it in the loop from 5 to n, and if any number divide it we will return false.

Here is the code after the enhancement:

def is_prime2(num):

if num %2==0 : # pass the even numbers.

return False

num_d= str(num) # if last digits is 5, then not prime

t= len(num_d)

if (num_d[t-1]) == 5 :

return False

tot = 0

for each in str(num):

tot = tot + int(each)

if tot % 3 == 0 : # if digits sum divide by 3, then not prime

return False

for t in range (3, num, 2):

if num % t == 0 :

return False

return True

I test both function on my laptop, for different number ranges, and use the time function to see the time delays with each one. Here is the results. If any one know better way to do this please drop it here. Or on My Twitter.

Follow me on Twitter..

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”

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
# Completed on Fri, 5 Jul 2019, 08:53

palin =0
def palindromic(n) :


for each in str(n) :


n_last = len(n_list)-1

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

Follow me on Twitter..

Python: Champernowne’s constant

Python: Champernowne’s constant
Problem No.40 @ ProjectEuler
Completed on: Mon, 1 Jul 2019, 18:01

In This task No.40, basically we need to get some digits from a large decimal fraction, then finding the multiplication of those digits.

ProjectEuler assume that the fraction is: 0.123456789101112131415161718192021222324 …. until 1000000, then we should fined the digits in positions 1, and 10, 100, 1000, 10000, 100000 and 1000000. Here is a copy of the problem screen

So to solve this I create a string variable n_list then using for loop i store the numbers from 1 to 1000000 in it as [12345678910111213141516 … 1000000], and simply get the digits I want using the list index, and Finally I calculate the needed multiplication as required. .. And we solve it. ..

Follow me on Twitter..

Python: Combinatoric Selections

Python: Combinatoric Selections
Problem 53 @ projecteuler
Completed on: Mon, 1 Jul 2019, 11:32

This is one of the tasks that based on equations. To solve this, first let me Simplify it.

Simplifying the task:

if 1<= n <= 100 , and r <= n

using the equation of C(n,r) is

c(n,r) = n! / (r! *(n-r)!

How many values of c(n,r) greater than one-million

Since we need the factorial of n (n!) and (r!) then I will re-use our function get_factorial(num), and I will write another function to calculate the C(n,r) as in the equation above, I will call it combi(n,r), also I will use a while loop, and we need to set some variables to help us in the execution. In this part, I will use a list called values_over_1m to store all the values in it, although this is Not required in the Problem 53, but i will keep it so later we can print them all to the screen or just get the total values of it. And so we did .. and we get the right answer.

The Code:

# Combinatoric selections
# Problem 53 @ projecteuler
# Completed on Mon, 1 Jul 2019, 11:32

if 1<= n <= 100 , and r <= n

using the equation of C(n,r) is
c(n,r) = n! / (r! *(n-r)!

How many values of c(n,r) greater than one-million


# Function to get the Factorials of a number.
def get_factorial(num):

if (num == 0) :

return 1


return num * get_factorial(num-1)

def combi(n,r):

fn = get_factorial(n)

fr = get_factorial(r)

fnr = get_factorial(n-r)

return fn / (fr*fnr)

# To store the values more than 1 million.
values_over_1m =[]

nn = 1
rr = 1

start_value= combi(nn,rr)
stop_me = False

while stop_me == False :

nn +=1

rr =2

while rr < = nn :

start_value = combi(nn,rr)

if start_value >1000000 :


rr +=1

if nn > 99 :

stop_me = True

print(‘Ther is ‘,len(values_over_1m),’ Values meeting the equation.’)

Follow me on Twitter..

Python: Permuted Multiples

Python: Permuted Multiples
Problem No.52 @ Projecteuler

This task Completed on Mon, 1 Jul 2019, 06:08, the goal is to find a number X that if we get the X, X*2, X*3, X*4, X*5, X*6 all the numbers are same digits but in a different order.

I use a function called if_x_in_y (n1,n2) will return True if the two numbers has same digits, False if not. Then a while found_one is not True a loop will examine all numbers starting from 2 until we found_one.

The Code:

# Permuted multiples
# Problem No.52
# Completed on Mon, 1 Jul 2019, 06:08am (GMT+3)

# if x, x*2, x*3, x*4, x*5, x*6 has the same digits.

def if_x_in_y (n1,n2) :

for x in str(n1) :

if x not in str(n2) :

return False

for x in str(n2):

if x not in str(n1):

return False

return True

x_num = 2
found_one= False

while not found_one:

if if_x_in_y(x_num,x_num*2):

if if_x_in_y(x_num,x_num*3):

if if_x_in_y(x_num,x_num*4):

if if_x_in_y(x_num,x_num*5):

if if_x_in_y(x_num,x_num*6):

print(‘\n Yes, we found one ..’)

found_one = True

print(‘ The Number is ‘,x_num)

x_num += 1


Follow me on Twitter..