5
$\begingroup$

I want to show that "Out of the first 450 Fibonacci numbers, the odd number is twice as many as even number." with Mathematica.

Can you solve it please?

$\endgroup$
3
  • 4
    $\begingroup$ Tally@Mod[Fibonacci@Range@450, 2] $\endgroup$ Commented Apr 3, 2020 at 16:48
  • 3
    $\begingroup$ Actually, $F_n$ is even iff $n$ is a multiple of $3$. $\endgroup$ Commented Apr 4, 2020 at 15:35
  • 2
    $\begingroup$ @lhf Yes, and by the construction principle of the Fibonacci number it is clear the the sequence contains always two consecutive odd numbers followed by an even one ;-) $\endgroup$ Commented Apr 21, 2020 at 12:09

6 Answers 6

10
$\begingroup$

First the Fibonacci numbers

out = Table[Fibonacci[n], {n, 450}]; 

then counting....

Mod[#, 2] & /@ out // Counts 

(<|1 -> 300, 0 -> 150|>)

$\endgroup$
1
  • 4
    $\begingroup$ I'd use CountsBy with EvenQ as the function instead of Mod[#, 2]&. The result is <|False -> 300, True -> 150|>. $\endgroup$ Commented Apr 4, 2020 at 3:17
8
$\begingroup$

Similar to mgamer's nice answer:

Mod[Array[Fibonacci, 450], 2]//Counts 

<|1 -> 300, 0 -> 150|>

Edit

To 'borrow' the neat suggestion made by rcollyer in a comment to rgamer's answer:

Array[Fibonacci, 450] // CountsBy[OddQ] 

<|True -> 300, False -> 150|>

Edit 2

lhf points out in a comment that "$F_n$ is even iff $n$ is a multiple of 3"

Table[Fibonacci[n], {n,3, 450,3}] // CountsBy[EvenQ] 
<|True -> 150|> 

A very interesting one, it seems to me. As long as $n$ is a multiple of 3, then "the odd number is twice as many as even number" as the OP asks for $n$ equal to 450.

$\endgroup$
8
$\begingroup$

Let's find the conditions under which a Fibonacci number is odd or even. Happily, Reduce has us covered:

isOdd = Reduce[Mod[Fibonacci[n], 2] == 1, n, Integers] (* Element[C[1], Integers] && (n == 1 + 3 C[1] || n == 2 + 3 C[1]) *) isEven = Reduce[Mod[Fibonacci[n], 2] == 0, n, Integers] (* Element[C[1], Integers] && (k == 0 && n == 3*C[1]) *) 

This says for all integers $ n $, $ F_n \equiv 0\,\left(\bmod 2\right) $ when $ n \equiv 0 \left(\bmod 3\right) $.

Now, let's see how many instances there are of each for $n$ between $1$ and $450$.

evens = Length@FindInstance[isEven && 1 <= n <= 450, {n, C[1]}, Integers, 450] (* 150 *) odds = Length@FindInstance[isOdd && 1 <= n <= 450, {n, C[1]}, Integers, 450] (* 300 *) 

Now that we've come this far, Mathematica can also help us with the basic arithmetic:

2 * evens == odds (* True *) 
$\endgroup$
4
$\begingroup$
Array[Fibonacci, 450, 1, CountsBy[OddQ] @* List] 
<|True -> 300, False -> 150|> 

Also

Array[Fibonacci /* OddQ, 450, 1, List /* Counts] 
<|True -> 300, False -> 150|> 

and

450 // Range /* Fibonacci /* CountsBy[OddQ] 
<|True -> 300, False -> 150|> 
$\endgroup$
1
$\begingroup$

Using GroupBy

GroupBy[Fibonacci @ Range @ 450, OddQ, Length] 

<|True -> 300, False -> 150|>

$\endgroup$
1
$\begingroup$

Just a Reap and Sow approach:

Reap[NestList[{#1, Sow[#2, Mod[#2, 2]]} & @@ ({{1, 1}, {1, 0}} . #) &, {1, 1}, 450][[All, 2]], _, #1 -> Length[#2] &][[2]] 

or using Fibonacci:

Reap[Table[ With[{f = Fibonacci[j]}, Sow[f, Mod[f, 2]]], {j, 450}], _, #1 -> Length@#2 &][[2]] 

Both yield {1 -> 300, 0 -> 150}

Or using GroupBy:

GroupBy[Fibonacci @ Range[450], Mod[#, 2] &, Length] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.