A selection of algorithms for computing the nth Fibonacci Number

One of these algorithms even gets as fast as ! Very impressive.

A selection of algorithms for computing the nth Fibonacci Number

One of these algorithms even gets as fast as ! Very impressive.

So fishes56 alerted me to this riddle in a comment here, and if you didn’t see it I wanted to share it here in a separate post cause its a little fun thing to think about.

12 is 6, 6 is 3, 3 is 5, 5 is 4, 4 is 4.

4 is the magic number, why?

68 is 10, 10 is 3, 3 is 5, 5 is 4, 4 is 4.

4 is the magic number, why?

26 is 9, 9 is 4, 4 is 4. 4 is the magic number, why?

Give it a try. 🙂

Here is a very simple math problem:

If a number is even, divide the number by two. If a number is odd, multiply the number by three, and then add one. Do this over and over with your results. Will you always get down to the value 1 no matter what number you choose?

Go ahead and test this out for yourself. Plug a few numbers in. Try it out. I’ll wait.

Back? Good. Here’s my example: I start with the number .

is even, so I divide it by .

.

.

is odd, so we multiply it by then add .

.

.

.

.

.

.

.

And we have arrived at one, just like we thought we would. But is this ALWAYS the case, for ANY number I could possibly dream of?

This may sound easy, but in fact it is an unsolved mathematics problem, with some of the best minds in the field doing research on it. It’s easy enough to check low values, but numbers are infinite, and can get very, very, very large. How do we KNOW that it works for every single number that exists?

That is what is so difficult about this problem. While every single number we have checked (which is a LOT) ends up at 1 sooner or later (some numbers can bounce around for a very long time, hence why they are called “hailstone sequences”), we still have no method to prove that it works for every number. Mathematicians have been looking for some method to predict this, but despite getting into some pretty heavy mathematics in an attempt to attack this problem, we still do not know for sure.

This problem is known as the Collatz Conjecture and is very interesting to mathematicians young and old because it is so easy to explain and play with, yet so tough to exhaustively prove. What do you think? Will this problem ever be solved? And what would be the implications if it was?

Here is some simple Python code that can display the cycles for any number you type in:

#!/usr/bin/python num = int(input("Enter a number: ")) while num!=1: if num%2 == 0: num = num/2 else: num = (num*3)+1 print num

Here’s something I was playing around with the other day.

Consider the Fibonacci sequence: . . I wanted to see if there was a pattern regarding the values of the ones digit in the fibonacci sequence. This is what I found:

The ones digits of the Fibonacci numbers are periodic, with period 60. This means that if you continue the sequence and look only at the ones digits of the numbers, they begin to repeat the same pattern after the 60th number. In addition, every 15th number in the sequence has a ones digit of zero. I wonder why this is. I can’t seem to find any other patterns at the moment. It probably has something to do with mod.

Here is the list of the 60 digit period (ones digits only):

1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0 9 9 8 7 5 2 7 9 6 5 1 6 7 3 0 3 3 6 9 5 4 9 3 2 5 7 2 9 1 0 ---this is where the cycle starts over 1 1 2 3 5 8 ...

Can you find any other interesting properties?

Inspiration for this problem comes from my good friend Johnny Aceti.

Consider numbers where is the set of all prime numbers .

**Conjecture 1. **There exist consecutive such that .

Is this true? Well lets see.

Every prime except 2 is odd. Let’s first look at adding together 2 odd primes. Odd numbers are equivalent to , and if one adds together two numbers that are , you get a sum that is . This means the sum is even, and since we can’t add two primes to get 2, this means that you cannot add an even number of odd primes to produce a prime. This takes out just about every possibility for this conjecture to be true, but we haven’t considered 2 yet, since before we were only working with odd primes.

Let’s try the case where we use 2 as .

and every other odd prime is . Therefore, if we added them, we could get a sum that is . So far so good, this means our sum is odd. But is it prime? The only instance involving 2 if have to be consecutive is . 5 is prime, and that becomes the only that satisfy Conjecture 1.

But what if didn’t have to be consecutive? Does the use of 2 still work?

Well, the short answer is **sometimes.**

Uh oh. 9 isn’t prime. Therefore, the use of 2 as or only works sometimes. This problem isn’t very interesting with only two variables though. Let’s add more…

Consider consecutive numbers where is the set of all prime numbers .

If we continue adding numbers in this way, how many times will the resulting sum be a prime? Is there any way to predict what triples of numbers produce primes and which do not?

Here are some more examples (all of which have prime sums):

Can we derive a pattern or a formula such that always holds?

Taking the fibonacci sequence mod 2 provides this pattern:

Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,…

Sequence mod 2: 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,…

Taking the fibonacci sequence mod 3 also produces an interesting pattern:

Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,…

Sequence mod 3: 1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2,…

Mod 4:

Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,…

Sequence mod 4: 1, 1, 2, 3, 1, 0, 1, 1, 2, 3, 1, 0, 1, 1,…

This can be continued for mod n.

Consider this table from Pisano Periods – Wikipedia :

(let be the period of fibonacci numbers mod n)

cycle | |||
---|---|---|---|

1 | 1 | 0 | |

2 | 3 | 011 | |

3 | 8 | 0112 0221 | |

4 | 6 | 011231 | |

5 | 20 | 01123 03314 04432 02241 | |

6 | 24 | 011235213415 055431453251 | |

7 | 16 | 01123516 06654261 | |

8 | 12 | 011235 055271 | |

9 | 24 | 011235843718 088764156281 | |

10 | 60 | 011235831459437 077415617853819 099875279651673 033695493257291 |

Note also:

- 2*3=6 which is
- 4*6=24 which is and
- 3*8=24 which is and
- the period for is 20, while the period for is 60. for , and , and ( according to OEIS)

Can we say that ?

Answer: **no.** provides a counter example because and . Therefore, where .

If we extend our table, we can also see that and . In this case, . This also works for , and possibly more. However and do not work. Can we say that ?

Answer: **no,** and do not work. Why are these special cases?

I’ll write a program to test this stuff more when I get home today.

So back in my previous post I discussed the problem of the Traveling Santa. He wants to deliver gifts as quickly as possible to seven different towns. At its essence, this is a Traveling Salesman Problem, a well known and very difficult problem in graph theory and combinatorial optimization. I decided to take my own crack at the problem and see if I could devise a solution.

I wrote a program to tell me the shortest Hamiltonian cycle through the 7 towns. I used the networkx library to set up my graphs and then generated a list of all the Hamiltonian cycles. Then using the weights provided by each edge I calculated the total distance for each of the paths. After that, I was able to narrow it down to the shortest ones.

One solution that my program found was the cycle that visits [7, 4, 2, 1, 5, 6, 3, 7] with a distance of 39. You can check out the code here, and I’d be interested to hear about your ideas or alternative implementations.