RE: [Golfing] Collatz Conjecture 08-21-2015, 08:46 PM
#12
(08-21-2015, 04:44 PM)Shebang Wrote: Well, it is a code-golf ;P I'll un-golf a little and explain it though!
Taking input (obviously)
Code:n=input()
Neat trick using complement and negation, ~-n is equivalent to n-1. Means I can remove the space between while and that. 0 is falsy, and 1-1 is zero, so once this hits one it will stop executing.
Code:while~-n:
This is using a trick with list indexing. n%2 will either return a 0 or 1, so I make a list of length 2 which has what I want to do based on that. An even number will return 0, so the 0th element should be n/2. An odd number will return 1, so the 1th element should be 3*n+1.
Code:n=[n/2,3*n+1][n%2]
Pretty obvious, prints whatever n is at.
Code:print n
Also, @Eclipse, my score is 45 now
This is quite clever to be honest!
However, why doesn't it get an error since Input() returns a string value, but you are operating with integers.
Tik Tak~! Time is up~!



![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)