0

I have the following piece of code:

A[m_, k_] := For[i = 1, i <= m, i++,
  For[j = 1, j <= k, j++,
   A[i_, j_] = If[i <= 0, 0,
     If[[i == 1 || i == 2] && j == 2, 1,
      If[OddQ[i]; j == 1, 1,
       If[! OddQ[i]; j == 1, 0,
        If[j == 2, A[i - 2, 2] + A[i - 3, 2], 
         If[i <= j, Fibonacci[i], 
          False]
         ]
        ]
       ]
      ]
     ]
   ]
  ]

When executing A[2, 3] I should get an answer that is equal to Fibonacci[2], but it does not work that way. Any suggestions ?

Sektor
  • 3,320
  • 7
  • 27
  • 36
shi
  • 1

1 Answers1

1

Ok, so this is the solution:

A[m_, k_] := For[i = 1, i <= m, i++,
 For[j = 1, j <= k, j++,
   A[i, j] =
    If[i <= 0, 0, 
     If[(i == 1 || i == 2) && j == 2, 1,
      If[OddQ[i]; j == 1, 1,
       If[! OddQ[i]; j == 1, 0,
        If[j == 2, A[i - 2, 2] + A[i - 3, 2], 
         If[i <= j, Fibonacci[i], 
          False]]]]]]]]

I found an error in one of the If statements:

[i == 1 || i == 2] && j == 2

should be

(i == 1 || i == 2) && j == 2

I subsequently changed

A[i_, j_] = 

to

A[i, j] =

in this way, instead of defining the function again, the program is saving the value of whatever A[i,j] is each time.

To visualize this in a grid, the function Grid should do the job.

Grid[Table[Table[A[i,j],{i,1,10}],{j,1,10}]]

in which the tens can be changed to any number. And you can also add the optional

...Frame -> All]

to the Grid function to make it nicer.