Fibonacci challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Fibonacci challenge

create a series of numbers in which each number is the multiple of the two preceding numbers. for example : 1, 2 , 2 ,4 ,8 ,32 ..... conditions : #Numbers should start from one #you can refer Fibonacci series as reference 0 ,1 ,1 ,2, 3 ,5, 8 ,13.......but these are sums of two preceding numbers #reach the highest number Go for it.....it might become headech

8th Sep 2017, 12:50 PM
Wahid Shaikh
Wahid Shaikh - avatar
29 Answers
+ 3
Your example is a little bit vague, so you mean start with 1 & 2?
8th Sep 2017, 1:27 PM
Zephyr Koo
Zephyr Koo - avatar
+ 3
You need at least two first term ^^, and if both are equals to 1, the serie will be infinite 1,1,....,1 More accurate to describe your serie as f(0) == 1, f(1) == n != 1, so serie is 1, n, 1*n, n*n, n*n*n, n*n*n*n*n... or rather: n^0, n^1, n^2, n^3, n^5, n^8... So you can say that your serie is rather done by: f(0) == n^0 == 1 f(1) == n^1 == n f(x) == f(x-2)*f(x-1) == n^(x-2) * n^(x-1) == n^((x-2)+(x-1)) == n^(2x-3) for x > 1 If I doesn't make mistake ;P (my mathematic skills were far far away ^^)
8th Sep 2017, 1:38 PM
visph
visph - avatar
+ 3
Well the sequence grow exponentially fast in this case, I can get up to 11th term only before the integer overflow. f(0) = 2^0 f(1) = 2^1 f(2) = 2^1 f(3) = 2^2 f(4) = 2^3 f(5) = 2^5 f(6) = 2^8 f(7) = 2^13 f(8) = 2^21 f(9) = 2^34 f(10) = 2^55 f(11) = 2^89 * 12th term failed to fit into 64-bit) 💡 Demo https://code.sololearn.com/cPvxMQAt80Ac/?ref=app
8th Sep 2017, 1:48 PM
Zephyr Koo
Zephyr Koo - avatar
+ 3
You can use recursion to make life easy.
8th Sep 2017, 2:24 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
def sequence(m): if m==1: return 2 elif m==2: return 4 else: return sequence(m-1)*sequence(m-2) #2 and 4 are example starting numbera
8th Sep 2017, 2:27 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
@visph Recursion is the easiest way here. It can work for any 2 inputted starting numbers
8th Sep 2017, 2:27 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
Cmon Visph you can do it! :)
8th Sep 2017, 2:49 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Hmm the next number going to generate is based on the previous 2 numbers, so we will start with f(0) = 2 // 1st term f(1) = 2 // 2nd term .. and so on Am I right?
8th Sep 2017, 1:11 PM
Zephyr Koo
Zephyr Koo - avatar
+ 2
that was just an example.... your first term should be 1 .....
8th Sep 2017, 1:17 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 2
ok ...your f(0) = 1 //1st term if that helps
8th Sep 2017, 1:29 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 2
"that was just an example" conditions specifically says numbers should "strat" From 1 (one). the second number can be anything ...... just give me the highest number and number of terms rather creating an infinite series ;)
8th Sep 2017, 2:14 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 2
you can try ..... or any different methods .....to do it but it should give you series ....as expected.
8th Sep 2017, 2:24 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 2
you miss the 4 ;( 1x2=2 2x2=4 4x2=8 8x4=32.......and so on the next value should be multiple of preceding two numbers
8th Sep 2017, 2:39 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 2
Arf! Not still working ^^ It must be fixed again... I will try ;)
8th Sep 2017, 2:49 PM
visph
visph - avatar
+ 2
#just enter the number of terms you want t=int(input("")) n1,n2=1,2 while(t>0): print(n1) n=n1*n2 n1=n2 n2=n t-=1; see the answer without any recursion © wahid shaikh
8th Sep 2017, 6:17 PM
Wahid Shaikh
Wahid Shaikh - avatar
+ 1
With Python, you doesn't have "highest" number, as Python natively handle big integers (stored as string, so theorically unlimited in size -- but not in memory to handle them ^^)
8th Sep 2017, 2:16 PM
visph
visph - avatar
+ 1
@$Vengat: No need for recursion, as the serie can be computed as a simple function ;) (but using only integer values, so not a continuous function ^^)
8th Sep 2017, 2:27 PM
visph
visph - avatar
+ 1
def sequence(n,m): if '.' in str(m) or '.' in str(n): print('Value Error') if m == 0: print(1) elif m == 1: print(n) else: print(n**(2*m-3)) for i in range (100): sequence(2,i)
8th Sep 2017, 2:37 PM
visph
visph - avatar
+ 1
@Wahid: Are you talking to me? My code only require the f(1) item, as f(0) is 1... run the code, and you will see that third value (f(2)) is 4 (if you give n==2 as f(1) item) ^^
8th Sep 2017, 2:43 PM
visph
visph - avatar
+ 1
# Wow... my mistake, that's not working for f(2), you're right: need to add an elif statement for f(2) case: def sequence(n,m): if '.' in str(m) or '.' in str(n): print('Value Error') if m == 0: print(1) elif m == 1: print(n) elif m == 2: print(n*n) else: print(n**(2*m-3)) for i in range (100): sequence(2,i)
8th Sep 2017, 2:47 PM
visph
visph - avatar