Archive

Posts Tagged ‘Language’

Python: The Factors

April 22, 2019 Leave a comment



Python: Factors of the Number N


This is a short task to get the factors of a given number. The Definition of Factors of N is: The pairs of numbers you multiply to get the N number.

For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4


In this task we will write a Python code to ask the user for a number N then will get all the pairs number that if we multiply them will get that N number, we will store the pairs in a array ‘factors’.



The Code:



def factors_of_n(num):

a=1

factors=[]

while a <= num:

if num%a==0:

if (num/a,a) not in factors:

factors.append((a,int(num/a)))

a = a + 1

return factors
#Ask the user for a number
num=int(input(“Enter a number: “))
print(factors_of_n(num))





Follow me on Twitter..



Advertisements

Python: Amicable Numbers

April 18, 2019 Leave a comment



Python: Amicable Numbers


Problem No.21 on projectEuler

In this task we need to calculate the SUM of all divisors of N, from 1 to N.
Then we calculate the Sum of (sum of N divisors ) let’s say M .
Now if

For example, the proper divisors of A=220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore F(220) = 284. And the proper divisors of B=284 are 1, 2, 4, 71 and 142; so F(284) = 220. So F(A) =B and F(B)=A then A and B are amicable.

Definition:

If f(a) = b and f(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.


In this task we will ask the user to enter a Range of numbers and we will search for all Amicable Numbers pairs in that range(from – to) if we fond one we will print out the number and the divisors list. The main function here is the one that get the sum of divisors, we will call it get_divisors_sum and we will examine Amicable with If statement.

Hint ..
To check if the sum of divisors from both said are equal we use (t2==x )
AND that this sum are not same we use (t1!=t2)
AND that we did not print the pair before we use: (t2 not in ami_pair)



The Code:



#Python: Amicable Numbers
#Problem No.21 on projectEuler

num1=int(input(‘The range Start from:’))
num2=int(input(‘The range Ends at:’))

t1=0
t2=0
l=[]
l1=[]
l2=[]
ami_pair=[]

def get_divisors_sum (num):

t=0

l=[]

for a in range (1,num):

if num%a==0 :

l.append(a)

t=t+a

return t,l

for x in range(num1,num2):

l1=[]

t1,l1=get_divisors_sum(x)

l2=[]

t2,l2=get_divisors_sum(t1)

if (t2==x) and (t1!=t2) and (t2 not in ami_pair):

print(‘\nget_divisors_sum({}) is {} divisors={}’.format(x,t1,l1))

print(‘\nget_divisors_sum({}) is {} divisors={}’.format(t1,t2,l2))

print(‘\nSo {} and {} are Amicable Numbers .’.format(t1,t2))

ami_pair.extend((t1,t2))







Follow me on Twitter..



Python Project: Sum of power of digits

April 17, 2019 Leave a comment



Python: Sum of power of digits


Problem No.16

In Problem No.16, projectEuler ask to find the Sum of power of digits in the number, for example if we have 2^15 (2 to power of 15) the answer is 23768 then we need to calculate the sum of this number (2+3+7+6+8 ) that’s equal to 26.

The Task: So our task in this project is to find the sum of the digits of (2^1000). To write this as a program and to make it more general we will ask the user to input the number and the power he want, then I start thinking to restrict user from input large numbers that could cause CPU problems, but then i decide to keep it open as is.



The Code:


num=2
p=15

num=int(input(“Enter a number “))
p=int(input(“Enter a power “))
a=num**p
def sum_of_digits(num,p):

a=num**p

l=[int(i) for i in str(a)]

print(l)

for x in range (len(l)):

t=t+l[x]

print (“sum =”,t)







Follow me on Twitter..



Python: Prime Numbers in Range

April 16, 2019 Leave a comment


Python: Prime Numbers in a Range

Our task here is simple as the title, we will have a range and will test each number to see if it is a prime then we will add it to a list, once we finish we will print out the list.

To complete this task we will use one of our function we create last time (Read: is prime post).

So, here we will ask the user to input two numbers num1 and num2 the we will pass all the numbers in the range to is_prime() and store the result in a list.



The Code:


#Function to get all Prime numbers in a range.
#Ask the user to enter two numbers
print ‘Get all Prime numbers in a range\n’
num1=int(input(“Enter the first number in the range: “))
num2=int(input(“Enter the last number in the range: “))
#create the list
prime_list=[]

def get_all_prime(num1,num2):

for x in range (num1,num2):

if is_prime(x) ==’Prime’:

prime_list.append(x)

return prime_list

#Call the function and print the list
get_all_prime(num1,num2)
print prime_list
print len(prime_list)







Follow me on Twitter..

Python Project

April 14, 2019 Leave a comment



Python: Sum of the Square and Square of the Sum


Difference of sum of the square and the square of the sum

I fond this on projecteuler.net Usually I add some steps to there problems to make it more application look and feel. Later on, we’ll see how.

Problem assumption: If we said that we have a range of numbers (1,10) then the sum of this range is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55, Now the Square of the sum is 55^2; thats mean the Square of the sum of (55) = 3025.

And for the same range, the Sum of Square means that we will get the Square of each number in the range then will get there summation. So with our range (1,10) Sum of Square is 1^2 + 2^2 + 3^2 + 4^2 + 5^2 …. + 10^2 = 385

The Problem #6 in ProjectEuler

The sum of the squares of the first ten natural numbers is,12 + 22 + … + 102= 385

The square of the sum of the first ten natural numbers is,(1 + 2 + … + 10)2 = 552= 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

The Task:Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


Inhancment Now to make this task workking as application and to get more general output of it, first we will ask the user to input a range of the numbers, then we will applay the function on that range.



The Code:



# Problem 6 in ProjectEuler.

def square_of_sum(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+x

print(‘The Square of the Sun’,tot*tot)

return tot*tot

def sum_of_square(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+(x*x)

print(‘The Sum of the Square ‘, tot)

return tot

#Ask the user for his input.
num1 = int(input(‘Enter First number in the range: ‘))
num2 = int(input(‘Enter the last number in the range: ‘))

#Call the functions.
w1 = square_of_sum(num1, num2)
w2 = sum_of_square(num1, num2)

#Output the Difference.
print(‘The Difference is: ‘, w1-w2)









Follow me on Twitter..



Python Project

April 10, 2019 1 comment


Python: Prime Numbera


Is it Prime

In a simple way, a Prime number is a number that cannot be made by multiplying other numbers. So 4 is Not Prime because we can say that 4 = 2 x 2, 6 is Not Prime because it can be produced by multiplying 3 x 2; but 5 Is Prime because we can’t find any integer numbers that can produce 5.

Numbers such as 1, 3, 5, 7, 11, 13 … all are Prime Numbers.


This function will ask the user to write a number then we will examine it to see whether it is a prime or not.



The Code:


num=int(input(“Enter a number: “))
def is_prime(num):

result=”Prime”

for t in range (2,num):

if num%t==0 :

result=”Not Prime”

break

return result
print num,’is’,is_prime(num)








Follow me on Twitter..

Python Project

April 10, 2019 2 comments



Python: Fibonacci Numbers


Even Fibonacci Numbers

Fibonacci Sequence is generated by adding the previous two terms, so the N number in the sequence will be Xn=(Xn-1) + (Xn-2). Example: by starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

The Task: Get the Fibonacci sequence range from the user and count how many even valued terms is there. The user range should not be more than 10000 number.



The Code:


def get_Fibonacci_Sequence(rfrom,rto):
n1=rfrom
n2=rfrom+1
count=0
for x in range (rfrom,rto):

fab=n1+n2

n1=n2

n2=fab

# Check if the fab is even, the increment the counter

if fab%2==0 :

count =count +1
# After finish print the output
print”Between”,rfrom,” and “,rto,” there is “,count,”even Fibonacci number.”

range_ok =”no”
while range_ok==”no”:

print(‘Your range must be less than 10,000:’)

# Get the numbers from the user.

rfrom=int(input(‘Enter the range From:’))

rto=int(input(‘Enter the range To:’))

# Check if the numbers are less than 10000

if (rto – rfrom) < 10000 : range_ok ="yes"

#Call the function
get_Fibonacci_Sequence(int(rfrom),int(rto))






Follow me on Twitter..