Archive

Posts Tagged ‘doha’

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

Advertisements

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