# Is there a way to implement

**URL:** <https://community.wolfram.com/t/is-there-a-way-to-implement/1699>\
**Category:** Questions\
**Tags:** mathematica, discrete-mathematics\
**Created:** [March 12, 2014, 4:38pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699 "2014-03-12T16:38:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lufux](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lufux](https://community.wolfram.com/u/lufux)\
**Post date:** [March 12, 2014, 4:38pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699/1 "2014-03-12T16:38:27Z")

</div>

Hey everyone,

im trying to implement a function which looks like this:

```auto
c[n_]:=Piecewise[{{c[n/3]+c[Abs[(n-2)/3-2]],Mod[n,6]==0&&n!=0},{c[(n - 2)/3] + c[Abs[(n - 2)/3 - 2]]),Mod[n,6]==2},...etc}]

```

Which uniquely defines its value at each n, since c[0] is defined to be 1. But Mathematica wont calculate the values and also the use of RecurrenceTable is restricted to arguments of type c[n+int].

A helpful idea would give me a happy day 🙂

Thanks for reading,

Lufu

---

<div class="post-metadata">

**Author:** ![rowland](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/rowland/32/36108_2.png) [@rowland](https://community.wolfram.com/u/rowland)\
**Post date:** [March 12, 2014, 5:25pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699/2 "2014-03-12T17:25:24Z")

</div>

You can do this sort of thing

```auto
f[1] = 7;
f[n_] := 3 f[Floor[n/3] + 1];
Array[f, 20]

```

although it is usually more efficient to store incrementally

```auto
f[1] = 7;
f[n_] := f[n]=3 f[Floor[n/3] + 1];
Array[f, 20]

```

Note the [generalized recursive functions](http://www.wolframscience.com/nksonline/page-130) from the New Kind of Science book and this note at the end  
[http://www.wolframscience.com/nksonline/page-907b-text](http://www.wolframscience.com/nksonline/page-907b-text)

---

<div class="post-metadata">

**Author:** ![lufux](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lufux](https://community.wolfram.com/u/lufux)\
**Post date:** [March 13, 2014, 1:11pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699/3 "2014-03-13T13:11:59Z")

</div>

This looks quite nice but doesnt work out with my function either. I think the problem is, that it defines the values somewhat implicit. Like f[2]=-f[0]-f[2], so mathematica got to solve a equation to get the value for f[2]. I attached the function to this post. Its even worse for the odd numbers because it refers to elements ahead, but overall everything is well defined.

---

<div class="post-metadata">

**Author:** ![rowland](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/rowland/32/36108_2.png) [@rowland](https://community.wolfram.com/u/rowland)\
**Post date:** [March 13, 2014, 2:03pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699/4 "2014-03-13T14:03:43Z")

</div>

I see. You get e.g., f[1]=f[2]+f[3] which makes f[1] look undefined but you also get f[2] == -f[0] - f[2], f[3] = f[0] + f[6], f[4] = -f[2] - f[4], f[6] = f[0] + f[2] + f[4] i.e. exactly enough to solve uniquely. The same problem happens with the enumerating [generalized recursive functions](http://www.wolframscience.com/nksonline/page-130). Some are nice, some undefined, some underdefined, but also some whose definition status is unclear and probably formally undecidable. These were studied by several people at the first NKS summer school (now the scope of the school is expanded with the new name [Wolfram Science Summer School](http://www.wolframscience.com/summerschool/)).

Here is one way to approach this, by generating the equations and using Reduce.

```auto
Clear[equ, f]; equ[0] = (f[0] == 1);
equ[n_] :=
f[n] == Which[Mod[n, 6] == 0 && n != 0,
   f[n/3] + f[Abs[n/3 - 2]] + f[Abs[n/3 + 2]],
   Mod[n, 6] == 2, -(f[(n - 2)/3] + f[Abs[(n - 2)/3 - 2]]) ,
   Mod[n, 6] == 4, -(f[(n + 2)/3] + f[Abs[(n + 2)/3 + 2]]) ,
   Mod[n, 2] == 1, f[Abs[n - 3]] + f[n + 3] ]
equs20 = Array[equ, 20, 0]

```

Array[f, 20] /. ToRules[Reduce]

---

<div class="post-metadata">

**Author:** ![lufux](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lufux](https://community.wolfram.com/u/lufux)\
**Post date:** [March 13, 2014, 2:52pm UTC](https://community.wolfram.com/t/is-there-a-way-to-implement/1699/5 "2014-03-13T14:52:49Z")

</div>

Great ! Thats exactly what i was looking for 🙂

Thanks a lot !
