← Back to challenges

A Normal Sequence

JavaScriptHardalgorithmslogicmathnumbers

Instructions

Consider a sequence where the first two numbers are 0 and 1 and the next number of the sequence is the sum of the previous two numbers modulo three.

Create a function that finds the nth element of the sequence.

Examples

normalSequence(1) ➞ 0

normalSequence(2) ➞ 1

normalSequence(3) ➞ 1
// (0+1)%3 = 1

normalSequence(20) ➞ 2

Notes

  • 1 ≤ N ≤ 10^19
  • A hint in comments section.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Merge Arrays in Order