Fibonacci numbers integer taken [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 questions about Fibonacci numbers.
1) The number of integers taken as an argument into the Fibonacci base functions? (in Turkish: Argüman olarak alınan onluk tabanda ki sayıyı Fibonacci tabanına dönüştüren işlev?)
2) The number given in the Fibonacci function converts integer base? (in Turkish: Fibonacci tabanında verilmiş sayıyı onluk tabana dönüştüren işlev?)
a, b = 0, 1
print (a)
print (b)
i = 2
while i<=50:
a,b = b, a+b
print (b)
i+=1
i can wrote that, but i want such as something like f(10) or f(5)
I'm sorry, my english isn't so good. Thank you.

Just move it all to a function and handle special cases.
def fib(n):
#TODO: handle invalid case(negative or non-int) and return
if(n == 0):
print(0)
return
if(n == 1):
print(1)
return
a, b = 0, 1
i = 2
#this will go up to the n-th number
while i<=n:
a,b = b, a+b
i+=1
print (b)
Then you can do this:
fib(1)
fib(0)
fib(6)
fib(33)
And you'll get:
1
0
8
3524578

Related

How to divide a number by 10 only in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I just need to divide some number by only 10. For instance, if the number is 127, the result should only be 12 and remained to be 7. Is there a way I can get Python to give me these BOTH results? Because I need to divide that remainder by some number as well for instance 2.
So overall it should be like this:
127 = 12 (10) and 3 (2) and 1(remainder).
Hope that makes sense
You can try using double division and modulus division -
quotient = 127 // 10
remainder = 127 % 10
print(quotient) # 12
print(remainder) # 7
Or use divmod -
num = 127
d = 10 # Divisor
quotient,remainder = divmod(num, d) # It returns tuple
print(quotient) # 12
print(remainder) # 7
def divide_and_get_remainder(num, divisor):
quotient = num // divisor
remainder = num % divisor
return quotient, remainder
if __name__ == '__main__':
num = 127
divisor = 10
q, r = divide_and_get_remainder(num,divisor)
print(f'{num} / {divisor} = {q} with remainder = {r}')

hi i m trying to find all the prime numbers from 1 to 1000 and find their max diffirence [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I already got the following part and I think its a fast and clear way and I dont need it to be faster:
for num in range(2,1000):
if all(num%i!=0 for i in range(2,num)):
print num
But how can I find the max difference of all the prime numbers I got and put that as the final result?
You can proceed like that:
biggest_prime = 2
for num in range(2,1000):
if all(num%i!=0 for i in range(2,num)):
biggest_prime = num
print "Difference: " + str(biggest_prime - 2) # outputs 995
Basically, you just store the last found prime, and when you're out of the loop you will have the value of the highest prime below 1000. You can then substract 2 to it.
If you don't like the fact that the 2is hard-coded, you can add a second variable lowest_prime that will change value only once.
Don't output each prime; store it in a list, so that once you have the full list, you can do what you like with the primes.
primes = []
for num in range(2, 1000):
if all(num % i != 0 for i in range(2, num)):
primes.append(num)
More briefly,
primes = [num for num in range(2, 1000) if all(num % i != 0 for i in range(2, num)]
primes = []
for num in range(2, 1000):
if all(num % i != 0 for i in range(2, num)):
primes.append(num)
x = primes[-1] - primes[0]
print(x)
so is this okay?

Writing a program that estimates math constant e without using any functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Write a program that estimates the value of the mathematical constant e by using the formula
[Note: Your program can stop after summing 10 terms.]
e = 1 + 1/1! + 1/2! + 1/3! + ...
I am new to programming languages and trying to learn python by myself. This question has been asked and answered before but I am seeking a solution without any function or module and I want to use only while or for loop.
Edit: This is the code that I wrote for calculating a factorial:
n = 0
factorial = 1
n = raw_input( "Enter a positive integer: ")
n = int(n)
while n < 0:
print "Enter a POSITIVE integer: "
backup = n
backup = int(backup)
while n != 0:
factorial *= n
n -= 1
print "%d!= %d" % (backup, factorial)
And this could be funny to most of you but this is the code that I wrote for the question but it ended up with a syntax error:
accuracy = 1
factorial = 1
counter = 1
e = 1.0
accuracy = raw_input( "Enter desired accuracy of e: ")
accuracy = int (accuracy)
while (counter <= (accuracy - 1))
factorial = factorial * counter
e = e + ( 1 / float(factorial))
counter += 1
print "Constant e is: ", e
Your first step is writing a factorial function. This can be done recursively or with a for-loop:
def factorial(n):
return 1 if n < 2 else n * factorial(n-1)
or:
def factorial(n):
x = 1
for i in range(2, n+1):
x *= i
return x
and then for calculating e we can again use recursion or a for-loop. This function takes a parameter which indicates how many terms in the formula it should evaluate (so you should call with 10 in your case).
def calc_e(t):
if not t:
return 0
else:
return 1 / factorial(t-1) + calc_e(t-1)
or:
def calc_e(t):
s = 0
for i in range(t):
s += 1 / factorial(i)
return s
and they both work:
>>> calc_e(1)
1.0
>>> calc_e(2)
2.0
>>> calc_e(3)
2.5
>>> calc_e(4)
2.6666666666666665
>>> calc_e(5)
2.708333333333333
>>> calc_e(10)
2.7182815255731922
>>> calc_e(20)
2.7182818284590455

Python:Project Euler #1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I've tried looking up some questions that were already on StackOverflow for project Euler problem 1 but not much has helped me. This is my code, it runs, but it's not correct. not sure whats wrong?
def sumOfMultiples(number):
sum = 0
for i in range(1,number):
if i % 3 == 0 or i % 5 == 0:
sum += 1
return sum
print (sumOfMultiples(15))
You are not using i at all. When the number meets the condition (divisible by 3 or 5), you want to add it, not 1.
sum += 1
should be
sum += i
The question asks for "the sum of all the multiples of 3 or 5 below 1000". You're adding 1 to the sum instead of adding the multiple of 3 or 5 to the sum.
So sum += 1 should be sum += i.
import time
#Recording start time of the program
start=time.time()
#A variable to store the sum
result=0
#calculating the sum for all the numbers below 1000
for i in range(1,1000):
#Checking the the condition
if i%3==0 or i%5==0:
result=result+i
print("Sum of all numbers that are multiples of 3 or 5 below 1000 is",result)
print("Execution time of program is",time.time()-start)
def sumOfMultiples(number):
sum = 0
for i in range(1,number):
if i % 3 == 0 or i % 5 == 0:
sum += i
return sum
print (sumOfMultiples(15))
In place of sum+=1 write sum+=i.
Here is a single-liner for you to ponder:
print sum([x for x in xrange(1,1000) if x%3==0 or x%5==0])
or you could also do it like this:
print sum([[0,x][x%3==0 or x%5==0] for x in xrange(1,10**3)])
try using this:
def sumOfMultiples(number):
sum = 0
for i in range(1,number):
if i % 3 == 0 or i % 5 == 0:
sum += i ## You needed to add i, not 1. Probably just a type-o as they are similar looking characters.
print(sum)
(sumOfMultiples(1000)) ## Also Euler1 requires this input to be 1000 for correct answer.

finding prime numbers greater than 10^12 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the sum of all prime numbers between 1,000,000,000,000 and 1,000,000,100,000?
this works out but is very slow.I need to optimize it.I am new to python.
3614000181007876 is the right answer
A=10 ** 6
N=A+1
B=10 ** 5
prime=[]
sum=0
for i in range(0,N):
prime.append(0)
for i in range(2,N):
if(prime[i]==1):
continue
for j in range(i*i,N,i):
prime[j]=1
for i in range((A ** 2)+1,(A ** 2)+B,2):
for j in range(2,A):
c=0
if(prime[j]==1):
continue
if(i%j==0):
c=c+1
if(c>0):
break
if(c==0):
#print(i)
sum=sum+i
print(sum)
Not the most efficient way, but gets the right result (hopefully 3614000181007876) in 2 seconds on my box:
def isPrime(n):
d = n - 1
s = 0
while not d & 1:
s += 1
d >>= 1
for a in (2, 13, 23, 1662803):
if pow(a, d, n) != 1 and all(pow(a, (1 << r) * d, n) != n - 1 for r in range(0, s)):
return False
return True
print(sum(x for x in range(1000000000001, 1000000100000, 2) if isPrime(x)))

Categories

Resources