← Back to challenges

A Normal Sequence

PythonHardalgorithmslogicmathnumbers

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

normal_sequence(1) ➞ 0

normal_sequence(2) ➞ 1

normal_sequence(3) ➞ 1
# (0+1)%3 = 1

normal_sequence(20) ➞ 2

Notes

  • 1 ≤ N ≤ 10^19
  • A hint in comments section.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.