Weird Algorithm

Problem

source: https://cses.fi/

Solution.

An important thing to note here is we should use long long n instead of simple int n because the updated value of n may become more than the upper bound because of multiplication.