2

Before compiling my code, I got stuck while generating a general formula. Can anyone help me get the general formula of the sequence below?

when n=3 the value is 0
when n=4 the value is 1
when n=5 the value is 0
when n=6 the value is -1
when n=7 the value is 0
when n=8 the value is 1
when n=9 the value is 0
when n=10 the value is-1
when n=11 the value is 0
when n=12 the value is 1
when n=13 the value is 0
 when n=14 the value is -1 etc

I got part of the formula but I couldn't determine what exactly should go where my question marks (???) are,

|(-1)^n -1|/2 + (-1)^[n(???/2)]
miradulo
  • 28,857
  • 6
  • 80
  • 93
babs
  • 31
  • 6

3 Answers3

4

Here's one general formula of the many, leveraging the periodicity of the sequence.

trigonometric approach

See the sequence on OEIS (A056594).


Another representation more along the lines of what you were attempting is using two "indicators", the first handling the parity and the second the sign for each even number.

indicator approach

miradulo
  • 28,857
  • 6
  • 80
  • 93
2

I hope this satisfies your request,

( (-1)^n + (1)^n )/2 *((-1)^(n/2))

intuition,
        -1  for n=6,10,14 ..
        0   for n=3,5,7,9 .. 
        1   for n=4,8,12 .. 

the first term reduces to zero whenever power is odd . in case of even power , we observe -1 has n/2 powers as odd .

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
arjunkhera
  • 929
  • 6
  • 23
  • There's no need to raise 1 to the power of n in your approach, anyhow nice answer! – miradulo Jul 13 '16 at 20:57
  • I agree , somehow overlooked it :) – arjunkhera Jul 14 '16 at 12:54
  • In case you aren't aware - if you choose to you can always edit your answer with the tool-tip underneath it. Furthermore, you might find the [Markdown editing help](http://stackoverflow.com/editing-help) useful for future answers. Welcome to Stack Overflow :) – miradulo Jul 14 '16 at 12:57
0

This seems simpler (to me, anyway):

((n+1)%2)*(2-(n+1)%4)
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101