Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Qupla
- // seq23 calculates and prints sequence x(n+1) = isEven(x(n)) ? x(n)/2 : 3*x(n)+1
- // It is a proven fact (by experiment) that with any positive integer x(0) < 100.000.000
- // this sequence always reaches 1 after finite number of steps.
- // Maybe someone already proved it for any positive integer :)
- // E.g. starting with x(0) = 27 sequence comes to value 1 after 110 steps.
- // Run with -eval
- // (c) lunfardo
- template seq23<T> {
- func T seq23<T>(T n){
- dummy = print<T>(n)
- is2 = equal<T>(n, as<T>(1))
- return is2 ? as<T>(2) : seq23do<T>(n)
- }
- func T seq23do<T>(T n){
- qr = divMod<T>(n, as<T>(2))
- return isZero[sign<T>(qr.remainder)]
- ? seq23<T>(qr.quotient)
- : seq23<T>(mul3add1(n))
- }
- func T mul3add1(T n){
- mul3 = rshift<T>(n)
- return add<T>(mul3, as<T>(1))
- }
- }
- // 110 steps
- eval seq23<Int>(27)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement