Fibonacci sequence calculation in Python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I tried to do a function that builds the Fibonacci series but when I try to check the calculation comes out wrong
def fibo(n):
i=1
j=1
for n in range(1,n):
j=j+i
i=j+i
return i+j
n=input('Enter number:')
print(fibo(int(n)))

i, j = 1, 1
for _ in range(n):
j = j + i
i = j + i
This is not the fibonacci sequence. Instead, try:
i, j = 1, 1
for _ in range(n):
i, j = j, i+j

Related

How to write a python program that takes a number from the users and prints the divisors of that number and then print how many divisors were there? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
This is my code so far
n=int(input('Enter the value of n: '))
for i in range(n):
if(n%i==0):
print(i, end=',')
I don't know how to find the number of divisors so i do the first part of the question. Please help me to find it.
You could define a list variable and append to this one if the condition is met:
n = int(input('Enter the value of n: '))
divisors = []
for i in range(1, n):
if n % i == 0:
divisors.append(i)
print(divisors)
print(len(divisors))
Now, len(divisors) is the number of divisors.
E.g. with an input of 100, the script yields
[1, 2, 4, 5, 10, 20, 25, 50]
8
Get accustomed to list comprehensions in Python:
divisors = [i for i in range(1, n) if not n % i]
#I have solved it using string as I was asked to show a output added with comma
num = int(input('Enter the value of num: '))
div = ""
count=0
for j in range(1, num+1):
if num % j == 0:
count+=1
if j!=num:
div+=str(j)+", "
else:
div+=str(j)
print(div)
print("Total", count, "divisors")

Code to count how many integers are strictly larger than all the integers to their right using python [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 2 years ago.
Improve this question
Given the list of integers, X,
https://www.google.com/url?q=https://docs.google.com/document/d/1TjeNYpZ_PbdBISlJPF-_WqULBc1WpuYthLClovjB3Rs/edit?usp%3Dsharing&sa=D&ust=1594968749928000&usg=AFQjCNG8bAv1lX8pXr4CYcgaDfYFxcbgCg
I want to write code to count how many integers are strictly larger than all the integers to their right excluding the last digit since it doesn’t have a number to its right. E.g. for [2,3,1] the answer should be 1 while for [12,4,4,2,2,3] the answer is 2. I have no clue how to write the code. I will appreciate any guidance on how to proceed.
Here's a solution that does that in O(N), where N is the size of the list.
l = [12,4,4,2,2,3]
def max_so_far(l):
m = 0
for i in l[::-1]:
m = max(m, i)
yield m
sum([x>y for x,y in zip(l[-2::-1], max_so_far(l))])
How about this code?
mylist = list(map(int,input().split()))
count = 0
for i,item in enumerate(mylist):
if i == len(mylist) - 1: #here we excluded the last element for a check
break
check = max(mylist[i+1:])
if check < item:
count = count + 1
print(count)
I am understating this problem in two way:
counter = 0
for i in range(len(list_of_number)):
for j in range(i, len(list_of_number)):
if list_of_number[j] > list_of_number[i]:
counter += 1
Here, after it gets the first value, it scans all the list. The code that follow it will check only the number's right neighbour
counter = 0
for i in range(len(list_of_number)-1):
if list_of_numbers[i+1] > list_of_numbers[i]:
counter +=1
How about this?
def counter(L):
return sum([1 for i in range(len(L)-1) if L[i] < L[i+1]])

How do I print the total number of iterations of the inner loop that were completed at the end of the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is what I'm assigned to do:
I don't understand how to do the second half (Perfect number) of the rubric.
This is what I have so far:
def sumMultiple(num):
sum = 0
for i in range(1, num//2+1):
if (num % i == 0):
sum += i
return sum
for in in range(1, 100000):
if(sumMultiple(i) == i):
print(i)
You can use a global variable that you iterate in the inner loop.
iterationCount = 0
def sumMultiple(num):
global iterationCount
sum = 0
for i in range(1, num//2+1):
if num % i == 0:
sum += i
iterationCount++
return sum
for i in range(1, 1000):
if sumMultiple(i) == i:
print(i)
print("Total iterations ", iterationCount)

simplify the python code with while loop and if condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I am totally new for python3. I need to write some easy process as following. But wondering if there is anyway I can simplify the following code?
def dosomething ( i ):
print(i);
n = 12
i = 1
while n > 0:
if i == 6:
i = 5
dosomething( i )
i += 1
n -= 1
It's not clear what you're trying to do with this example or if your constants are significant, but here's one approach that creates the same output a bit more simply:
def dosomething ( i ):
print(i);
n = 12
i = 1
for j in range(1, 6):
dosomething(j)
for k in range(n - 5):
dosomething(5)
In general, if the number of iterations is known in advance, you should express it with a for loop rather than a while loop.
this would work, simplifying the while loop into a for loop:
i = 1
for n in range(1, 13):
if i == 6:
i = 5
dosomething(i)
i += 1
if you are trying to avoid dosomething(6):
for n in range(1, 13):
if n != 6:
dosomething(n)

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