https://scratch.mit.edu/discuss/post/5919692/ の解答です。 オイラー関数というものがあります。
解説 : https://scratch.mit.edu/discuss/post/5921761/