# Finding a single equation without use of words, only functions and math, that solves outputs patterns within patterns

If you were given a sequence that went something like $$1,1,3,2,5,3,7,4,9,5....$$ You would notice that there are two patterns alternating, one being 1,3,5,7,9... and the other being 1,2,3,4,5...

How would you define this sequence with one single function, $k(n)$,without the use of words or extra functions such as "If n is even, apply the function $f(n)=\frac{n}{2}$ and if n is odd, apply the function $f(n) = n$"

Is there an elegant way to output such a sequence? I tried to do it and have my own way using a lot of terms but want to know if there is a much simpler solution

Furthermore, if your particular equation works for this equation, would it work for $p=3,4,5,6...$ patterns within a pattern. In addition, the patterns rotate uniformly meaning if there were 4 patterns, every 4 terms the same function is used to output a number based on the term number: An example of 4 patterns in a pattern that follow the following functions $f(n)=n,f(n)=n^2,f(n)=3n,f(n)=\lfloor{\sqrt{n}}\rfloor$, a certain function $k_p(n)$ where $p=4$ would output $$1,4,9,2,$$$$5,36,21,2$$$$9,100,33,3...$$ (The spacing is just for visual purposes and makes it easier to see how each pattern rotates every 4 terms)

Whatever solution that outputs such a pattern should also be able to change or be manipulated easily to accommodate more or less patterns with functions for each pattern varying in complexity

Update: A common way that people are trying to use to solve the first example with only two simple patterns is through the use of the properties of $(-1)^n$ which outputs -1 or 1, an alternating sequence between two values, perfect for the first example. I hope that further answers to this question should somehow expand to the next part of it as I know there are more than enough equations that output the first pattern.

My Work: I tried using modulus arithmetic to output the values I wanted that would sort of turn off or turn on certain functions depending on the term $n$. For the first example, I made the equation $k_2(n)= (mod(n,2))*n + (mod(n+1,2))*(\frac{n}{2})$ I think further work can be done in improving the modulus method as there is a common number between the mod sections of $k_2(n)$ and the number of patterns $p$ which is 2

• I read some time ago about this topic, I think it was in the book Concrete mathematics of Graham and Knuth. If not it was in Generatingfunctionology. – Masacroso Apr 4 '17 at 8:38
• Thank you @Masacroso, I will look into those two – Stone Apr 4 '17 at 8:39
• This feels like code golf. en.wikipedia.org/wiki/Code_golf – Elizabeth S. Q. Goodman Apr 4 '17 at 8:46

Note that

$$f(x)=\frac12((-1)^x+1)$$ is zero when $x$ is odd and $1$ when $x$ is even, similarly, we can make such function that is zero when $x$ is even and $1$ when odd.

Thus,

$$1,1,3,2,5,3,7,4,9,5,...$$

Could be written as

$$k(x)=(x+1)\left(\frac12(1-(-1)^x)\right)+\frac{x}{2}\left(\frac12(1-(-1)^x)\right)$$

Which simplifies to

$$k(x)=\frac12+\frac{3x}4-\left(\frac12+\frac{3x}4\right)(-1)^x$$

In fact, you can do this with every power of $2$; simply take that first function $f$, and apply it on $f$, so that we have a function that is $1$ only if $x$ is divisible by $4$, etcetera. If we wish to do this with $k=3$, then it already becomes more complicated. We need a number that cycles with period $3$ when raising it to a power; well, an easy one would be $\omega_3=e^{\frac23i\pi}$, a cube root of $1$. Then we see

$$f_3(x)=\frac13\left(\omega_3^x+\omega_3^{2x}+\omega_3^{3x}\right)$$

has the exact property we want; it is $1$ only if $x$ is a multiple of $3$, and $0$ otherwise. We can in fact do this for every prime number $p$; define $\omega_p=e^{\frac 2pi\pi}$ such that

$$f_p(x)=\frac1p\left(\omega_p^x+\omega_p^{2x}+\cdots+\omega_p^{px}\right)$$

and then $f_p$ is $1$ only if $x$ is divisible by $p$, and $0$ otherwise. We can now create such function for every $n$, since if $n=p_1p_2\cdots p_k$, then $f_n(x)=(f_{p_1}f_{p_2}\cdots f_{p_k})(x)$.

• I in fact noticed this common pattern that I believe is sometimes used in computer science when alternating between two values or in this case, alternating between two functions. However, we can't exactly define new values for $(-1)^3$ or $(-1)^k$ that output something besides -1 and 1. Is there perhaps a better way that accommodates for more patterns? In addition, I prefer if we don't just map values to our liking, the solution would be much more elegant but if needed then please do so. – Stone Apr 4 '17 at 8:48
• "I prefer if we don't just map values to our liking..." what do you mean? Am I misunderstanding the question? – vrugtehagel Apr 4 '17 at 8:54
• So unless I don't know what mapping means, but things like mapping $(-1)^3$ to some other value that might help us solve much more complicated values. – Stone Apr 4 '17 at 9:03
• Does this method only work for a prime p? Can this work for composite numbers as well? It's quite a nice idea but is limited to prime numbers if I'm understanding you correctly – Stone Apr 4 '17 at 9:10
• Read the last line: "We can now create such function for every $n$..." – vrugtehagel Apr 4 '17 at 9:11

For this particular question : $$T_n=\frac{3n}{4} + (-1)^{n-1}\frac{n}{4}$$

• I'm pretty sure there are quite a few ways to solve the first part, but would this be possible to expand on? I noticed that this seems to be limited to just 2 patterns. Also i'm not quite sure what to do with n = 1, should we treat $(-1)^0 = 1$? – Stone Apr 4 '17 at 8:39
• Yes : $(-1)^{0}=1$, or you may replace $n-1 \rightarrow n+1$, would work the same.Frankly, I too don't know can this be expanded to more than $2$ terms or not. – Jaideep Khare Apr 4 '17 at 8:41

Using trigonometric functions:

$$f(n) = |\sin(\frac{n\pi }{2})|n + |\cos(\frac{n\pi }{2})| \frac{n}{2}$$

$$f(n)=\frac{n}{2-n+2\lfloor\frac{n}2\rfloor}$$ Not very elegant, though.

$$n\cdot\frac{n\bmod2+1}{2}.$$