Skip to main content
added 567 characters in body
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697

Haskell, 78 bytes

4 bytes saved thanks to nimi

a#b=a:b#(a-b) f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]$0#1;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up (#), (#) takes two parameters, a and b, and returns the a list starting with a and followed by b#(a-b). This creates an infinite list, but because Haskell is lazy we don't need to wory about it looping forever. This essentially works backwards creating the Fibonacci sequence before a certain pair. For example (0#1) would be the list of all the non-positively indexed fibonacci starting from those numbersFibonacci numbers with negative index.

From here we make f. f takes a argument a which is the number we are trying to find in the sequence. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list 0#11. Since the function a*a+1 grows faster than the inverse of the Fibonacci sequence we can be sure that if we check within this bound we will find all the results. This prevents us from searching an infinite list. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence so we return -i, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 78 bytes

a#b=a:b#(a-b) f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]$0#1;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up (#), (#) takes two parameters and returns the list of all the non-positively indexed fibonacci starting from those numbers numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list 0#11. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 78 bytes

4 bytes saved thanks to nimi

a#b=a:b#(a-b) f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]$0#1;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up (#), (#) takes two parameters, a and b, and returns the a list starting with a and followed by b#(a-b). This creates an infinite list, but because Haskell is lazy we don't need to wory about it looping forever. This essentially works backwards creating the Fibonacci sequence before a certain pair. For example (0#1) would be the list of all the Fibonacci numbers with negative index.

From here we make f. f takes a argument a which is the number we are trying to find in the sequence. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list 0#11. Since the function a*a+1 grows faster than the inverse of the Fibonacci sequence we can be sure that if we check within this bound we will find all the results. This prevents us from searching an infinite list. Then for each value x and index i, if x==a we found a in the negative half of the sequence so we return -i, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

added 60 characters in body
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697

Haskell, 8278 bytes

u=0a#b=a:scanl(flipb#(a-)b)1u f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]u;[a*a+1]$0#1;[-i|x==a]++[i|abs x==a]} 

Try it online!Try it online!

First we set up u(#), u(#) is atakes two parameters and returns the list of all the non-positively indexed fibonacci starting from those numbers numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list u0#11. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 82 bytes

u=0:scanl(flip(-))1u f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]u;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up u, u is a list of all the non-positively indexed fibonacci numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list u1. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 78 bytes

a#b=a:b#(a-b) f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]$0#1;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up (#), (#) takes two parameters and returns the list of all the non-positively indexed fibonacci starting from those numbers numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list 0#11. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

added 414 characters in body
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697

Haskell, 82 bytes

u=0:scanl(flip(-))1u f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]u;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up u, u is a list of all the non-positively indexed fibonacci numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list u1. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 82 bytes

u=0:scanl(flip(-))1u f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]u;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up u, u is a list of all the non-positively indexed fibonacci numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list u1. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

Haskell, 82 bytes

u=0:scanl(flip(-))1u f 0=[0] f a=do{(i,x)<-zip[0..a*a+1]u;[-i|x==a]++[i|abs x==a]} 

Try it online!

First we set up u, u is a list of all the non-positively indexed fibonacci numbers.

From here we make f. f takes a argument a. Here we use do notation to do a list comprehension. We start by taking the first a*a+1 elements of the list u1. Then for each value x and index i, if x==a we return -i because we found a in the negative half of the sequence, and if abs x==a we return i as well because the absolute value of the negative half is the positive half so we found it there.

Since this makes the list [0,0] for 0 we hardcode the correct output for that one.

1: This trick is taken from Οurous' Clean answer. The same speedup aplies here as there, replace a*a+1 with abs a+1 to save a lot of time.

deleted 7 characters in body
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697
Loading
deleted 17 characters in body
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697
Loading
Source Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697
Loading