How many Fibs?

Find the number of Fibonacci numbers F i F_i such that 1234567890 F i 9876543210 1234567890 \le F_i \le 9876543210 .


The answer is 4.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

6 solutions

phi = (1 + 5**0.5) / 2

 def fib(n):

    return int(round((phi**n - (1-phi)**n) / 5**0.5))

 for i in range(0,50):
    if 1234567890<= fib(i) and fib(i) <= 9876543210:
       print(i)

Not that elegant but very fast code.....

Masbahul Islam
Aug 24, 2016

def fib(n):

  f1=1
  f2=1
  if n==1 or n==2:
      return 1
 else:
     for i in range(3,n+1):
         f1,f2=f2,f1+f2
 return f2



 count=0
 for i in range(1,200):
       if fib(i)>=1234567890 and fib(i)<=9876543210:
      count+=1
 print count

4

Micah Wood
Dec 21, 2015

My code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
a = 1
b = 1
N = 0

while b < 9876543211:
  if b > 1234567889:
    N += 1
  temp = a+b
  a = b
  b = temp

print(N)

Aareyan Manzoor
Jun 21, 2015

python 3.4:

Jan Hrček
Jun 6, 2015

Solution using Haskell:

1
2
3
> let fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
> takeWhile (<=9876543210) $ dropWhile (<1234567890) fibs
> [1836311903,2971215073,4807526976,7778742049]

So the answer is 4 4

Eddie The Head
May 4, 2015

Here is how I coded it up

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def fib(n):
    k = 0
    m = 1
    for i in range(n):
        sub = m
        m = k+m
        k = sub
    return k

def fib_in_range(a,b):
    n=0
    num = 0
    while fib(n) <= b:
        if fib(n) >= a:
            num +=1
        n += 1
    return num

print(fib_in_range(1234567890,9876543210))

Welcome back,sir!!

Adarsh Kumar - 6 years, 1 month ago

Log in to reply

Thanks! :)

Eddie The Head - 6 years, 1 month ago

Log in to reply

Good to have you back!

Adarsh Kumar - 6 years, 1 month ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...