Fibonacci series in python giving odd results - python

I have started learning python and my first program on fibonacci started giving me some weird answer, I know I am missing conceptually something so need guide from some expert on this. My program looks like this
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a = b
b = a + b
output
1
2
4
8
16
32
But When i wrote like this I got correct result
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a,b = b, a + b
output:
1
1
2
3
5
8
13
21
34
Guide me pls

a,b = 0,1
a = b # a <- 1
b = a + b # b <- a + b (1 + 1 = 2)
That's two separate operations where the a in the final line has already been modified before use.
On the other hand:
a,b = b, a + b
is an atomic operation where everything on the right side of = is the original value.
Hence it's equivalent to:
a,b = 0,1
t = a # t <- 0
a = b # a <- 1
b = t + b # b <- t + b (0 + 1 = 1)

Related

which is the difference between the 2 codes [duplicate]

This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 3 years ago.
Seems the same code but i get the different results
# code1
a = 0
b = 1
for i in range(0, 10):
print(a)
a = b
b = a + b
# code2
a, b = 0, 1
for i in range(0, 10):
print(a)
a,b = b, a + b
a = 0
b = 1
for i in range(0, 10):
print(a)
a = b
b = a + b
print()
a, b = 0, 1
for i in range(0, 10):
print(a)
a,b = b, a + b
I expected the same output
When you use the code 1 : first, a takes the value of b and then b become a+b, but with the new value of a.
In code 2, the evaluation of a and b is "simultaneous", as you use unpacking. In the same time, a takeb and b takes the value of a+b, but a still have his initial value.
Hope that I'm clear !

How can i get sum of all the numbers that this program prints?

B = 1
A = 3
C = 1
while C < 1000:
B = B + 1
C = A * B
print (C)
This is the code and i want to get the sum of the numbers that it prints
Here is a possibility,
B = 1
A = 3
C = 1
D = 0
while C < 1000:
B = B + 1
C = A * B
D += C
print (C)
# sum of all printed numbers
print(D)
B runs over all the integers from 2 to 334; you only need the sum of all integers from 2 to 334 (which is well known: average * number of elements) and then multiply it by A:
A = 3
B_max = 334 # ~ (1000 // A) + ...
res = A * (B_max + 2) * (B_max - 1) // 2
# 167832
you just need to make sure you get B_max right...
there is no reason for a loop at all if that is all you need to do.
define a list outside while:
dataList = []
then add C in that list:
while C < 1000:
B = B + 1
C = A * B
dataList.append(C)
then find sum:
print(sum(dataList))
for me, your goal is not clear. can you explain it more for me to be able to help you?
PS. your B = B + 1 can be simplified to:
B += 1
You should declare SUM first:
SUM=0
And at the end of while loop after print message, add
SUM = SUM + C
And thats all, simplest possible way.
B = 1
A = 3
C = 1
total = 0
while C < 1000:
B = B + 1
C = A * B
print(C)
total+=C
print("Sum is : ",total)

Python (Fibo series): trying to understand what is the difference between a, b = b, a + b OR a = b & a = a + b

I am not sure what would be an appropriate heading for this question and this can be a repeated question as well. So please guide accordingly.
I am new to python programming. I have this simple code to generate Fibonacci series.
1: def fibo(n):
2: a = 0
3: b = 1
4: for x in range(n):
5: print (a, end=' ')
6: #a, b = b, a+b
7: a = b
8: b = a+b
9: print()
10: num = int(input("enter n value: "))
11: print(fibo(num))
If I execute the above code as-is the result I get is as follows
enter n value: 10
0 1 2 4 8 16 32 64 128 256
If uncomment #6 and comment lines #7 and #8 the result I get is the actual fibo series.
enter n value: 10
0 1 1 2 3 5 8 13 21 34
I would like to know what is the difference between
a, b = b, a + b
and
a = b
b = a + b
Programming IDE used: PyCharm Community 2017.3
a = b
b = a + b
is actually:
a = b
b = b + b
what you want is:
a = b
b = old_value_of_a + b
When you do
a, b = b, a + b
it really is doing:
tmp_a = b
tmp_b = a + b
a = tmp_a
b = tmp_b
which is what you want
In line 7, you've already assigned the value in b to a, so in line 8, new value for b is actually double the old b's value.
While in line 6, the values on the right side of = will be using the old values, that's why you could get Fibo series.
Assignment Statements assigns reference of source variable to target variable. Let walk through an example to understand more
>>> a = 5
>>> b = 6
>>> a = b
In this example b is source variable and a is the target variable. Now memory address for both of these variables are same. We can confirm this as well
>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x1002739e0')
Another test to confirm this is to use is operator
>>> a is b
>>> True
Now coming back to your example. First statement
>>> a, b = b, a + b
Assignes b to a and (a+b) to b. This happens as a single operation so both variables are different. We can apply above tests to confirm this
>>> a is b
>>> False
>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x2008739t0')
The second statement
>>> a = b
>>> b = a + b
Assignes b to a and then (a+b) to b. These are two different statements, so at first step a and b are already identical. Thus the second statement is equivalent to b = b + b.
Thought make it simple so anyone can understand it
if you use this kind of syntax
a = 10
b = 20
a = b
b = a+b
print (a)
print (b)
after initially assigning a = 10 it will be assigning a = 20 since python is dynamically typed language it will change the value of variable a from 10 to 20
so the result will be like
a=20
b=40
but if we use
a = 10
b = 20
a,b = b,a+b
print (a)
print (b)
this will be assigning the values in a single line so the values of a and b will be exactly used from what it is initialised above it and the result will be like
which is the correct solution
a=20
b=30
I think the # line is pythonic solution. But if you got confused,you can you use a variable which is temporary. you can assign the value temp before, then you can change the values

How to add iteration numbers

my question is about adding loops in python
I am trying to add iteration numbers like summation of all iteration results
For example
first iteration
a = 10
b = 5
a = a + b = 15
c = a = 15
second iteration
a = 15
b = 5
a = a + b = 20
c = a1 + a2 = 35
I know to iterate using for loop which is like this
for i in range(2)
a = 10
b = 5
a = a + b
which gives a = 20
but how to get c which is adding iteration results
a=10
b=5
c=0
for i in range(2):
a = a + b
c = c + a
print a
print c
Above one gives results of a and c
You can define your variable outside your for loop to update it with the sum of each iteration
a_values = [10, 15]
b_values = [5, 5]
c = 0
for a, b in zip(a_values, b_values):
c += a + b
print(c)
35
I am afraid you are quite confused OP. Why don't you just do this instead:
a = 10
b = 5
c = sum(a+b for _ in range(2))
The problem with your version is that after calculating a during the first iteration, you re-assign its value to 10 in the second. Your loop would work with some modifications:
a = 10
b = 5
c = 0
for i in range(2)
c += a + b
You actually don't need any loop if you apply some basic arithmetic:
print(a + n * b)
print(a * n + (n*(n+1))//2 * b)
With a = 10, b = 5 and n = 2, it outputs:
20
35

How to compute mutually recursive expressions in a loop?

I have two expressions:
a = 3 * b + c , with c = 1
b = (a - d) / 5 , with d = 1
I must calculate a in 1 starting with b = 1, then (with a just calculated) I must calculate b in 2.
if the subtraction of b in 1 and b in 2 is > 0,25
the loop continues calculating a in 1 again with the b value in 2 and so on,
otherwise it stops.
It needs two iterations and I think I must use the while loop but I can't write it down.
Especially I don't know how giving the instruction to take the b value calculated in 2 as the first step
of the following iteration.
It sounds like you mean this. Some kind of iterative function closing in on a value?
#!/usr/bin/env python3
c = 1
d = 1
b = 1
while True:
a = 3 * b + c
new_b = (a - d) / 5
if b - new_b <= 0.25:
break
b = new_b
print(a, new_b)
Roughly this:
b, c, d = [1]*3
while True:
a=3*b + c
b= (a - d)/5
if condition:
break
You just need to fill out condition

Categories

Resources