Search code examples
algorithmabstractpseudocode

Most succinct way to enumerate the zero crossings of a particular spiral


What is the most succinct way (in pseudocode) to create the following map f from the naturals to the integers f(0) = 0; f(1) = 1; f(2) = -1; f(3) = 2; f(4) = -2; f(5) = 3; etc

You can imagine them as the zero crossing of a double symmetric Archimedean spiral.

Oh, and no float math allowed! Float math would be... ugly in this situation.


Solution

  • Wolfram Alpha found a closed form for calculating the nth term directly:

    closed form

    The expression -1n can also be written as (n % 2 == 0 ? 1 : -1) if n is a positive integer.