11Example programs
The Collatz sequence
A simple rule, an unsolved puzzle, and two functions that share a helper.
Pick a whole number. If it is even, halve it. If it is odd, triple it and add 1. Repeat. Each number ever tried has reached 1 in the end, but no one has proved that this holds for all numbers. It is one of the most famous unsolved problems in mathematics.
Starting from 27, the sequence takes 111 steps and climbs as high as 9,232 before it falls to 1.
စံကား
Expected output
အဆင့် အရေအတွက်
၁၁၁
အမြင့်ဆုံး
၉၂၃၂
How it works
နောက်တစ်ဆင့်("next step") holds the rule in one place. For even numbers, it returns before the end.အဆင့်အရေအတွက်("number of steps") andအမြင့်ဆုံး("highest") both loop untilနreaches 1, and both callနောက်တစ်ဆင့်. Sharing the helper means the rule is written once.အဆင့်အရေအတွက်counts inရလဒ်, which starts at 0.အမြင့်ဆုံးstartsရလဒ်atနand raises it whenever a bigger number shows up.
Hands on
- Find the number below 100 that takes the most steps. (Use a counted loop and keep the best so far in two variables.)