# FIBonacci sequence

For this code golf, you will receive an input of a fibonacci sequence, that is, a normal Fibonacci sequence but with one number incorrect. See, the sequence is fibbing! Get it? :D

Your job is to find out which number is incorrect, and print the index (0-based) of that number.

For example:

Input : 1 1 2 9 5 8 13
Output: 3

Input : 8 13 21 34 55 80
Output: 5

Input : 2 3 5 5 13 21
Output: 3

Specifications:

• The sequence may start at any number.
• The first two numbers of the input will always be correct.
• Shortest code (character count) wins.
• Does the input have to be space-delimited or can commas be used as well? – Volatility Jun 23 '13 at 0:26
• @Volatility Input is space-delimited. – Doorknob Jun 23 '13 at 0:33
• The job is to find only the first such number, right? For example, if you started from the right in the first sequence you could think that 8 is incorrect because it doesn't equal 9+5 – Luis Mendo Jan 23 '15 at 16:40
• @LuisMendo There will always be only one such number. – Doorknob Jan 23 '15 at 16:48
• @LuisMendo Okay, let me reword that: There will always be exactly one way to change a single number that causes the sequence to be correct. – Doorknob Jan 23 '15 at 16:53

## GolfScript (18 chars)

~]:^,,{^>3<~-+}?2+

The key to keeping this short is ? (find).

• +1 for the portrait of Fibonacci ~]:^, – gnibbler Jun 26 '13 at 5:26

## J, 30 23

(2+0 i.~2&}.=[:}:}:+}.)

## Golfscript, 31282625 23

~]-1%~1{)\3$3$-=}do])\;

## APL (19)

1+1⍳⍨(1↓1⌽k)≠2+/k←⎕

Explanation:

• k←⎕: store user input in k
• 2+/k: sum each pair of elements in k (i.e. 1 1 2 3 -> 1+1 1+2 2+3 -> 2 3 5)
• 1↓1⌽k: rotate k to the right by 1 and then drop the first element (i.e. 1 1 2 3 -> 2 3 1)
• : find the place where these lists are not equal
• 1⍳⍨: find the location of the first 1 in this list (location of the incorrect number)
• 1+: add 1 to compensate for the dropped element

# K, 32

{2+*&~(n@n@x)=x+(n:{1_x,x 0})@x}

# dc, 36 32

?zszsasb[lalbdsa+dsb=x]dsxxlzz-p

dc is a reverse-Polish calculator, so obviously you need to input the numbers in reverse order ;)

$dc fib.dc <<< "999 13 8 5 3 2 1 1" 7$ dc fib.dc <<< "999 1 1"
2

# Javascript (69686160 55)

for(s=prompt(i=2).split(' ');s[i]-s[i-1]==s[i-2];i++);i

(60)

s=prompt(i=2).split(' ');for(;s[i]==+s[i-1]+ +s[i++-2];);--i

(61)

s=prompt(i=1).split(' ');for(k=+s[1];k+=+s[i-1],k==s[++i];);i

(68)

(69)

Try it online!

# QBIC, 31 bytes

_!_!{_!~a+b=c|a=b┘b=c┘s=s+1\_Xs

## Explanation

_!_!           Ask the user for two umbers, assign them to 'a' and 'b'
{              DO
_!            Ask for a third number (this will be assigned to 'c' on every iteration)
~a+b=c        IF the previous two terms add up to the third
|a=b          THEN shift b into a,
┘b=c            and c into b
┘s=s+1          increment s (starts as 3 in QBIC)
\_Xs          ELSE quit, printing the step counter

I'm not quite sure if this is allowed; the sequence is entered one term at a time, and the program aborts on error, not after entering the entire sequence.