Login Register


[Golfing] Collatz Conjecture filter_list
Author
Message
RE: [Golfing] Collatz Conjecture #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 Wink

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.


[Image: tumblr_noac9s6rgw1tvnnaxo1_500.gif]
Tik Tak~! Time is up~!

Reply





Messages In This Thread
[Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 04:09 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 04:26 PM
RE: [Golfing] Collatz Conjecture - by Jolly - 08-21-2015, 04:37 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 04:44 PM
RE: [Golfing] Collatz Conjecture - by Jolly - 08-21-2015, 08:46 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 09:13 PM
RE: [Golfing] Collatz Conjecture - by Equinox - 08-21-2015, 07:59 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 10:45 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 04:27 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 04:31 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 04:53 PM
RE: [Golfing] Collatz Conjecture - by lux - 08-21-2015, 06:10 PM
RE: [Golfing] Collatz Conjecture - by OversouL - 08-21-2015, 06:23 PM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-21-2015, 06:58 PM
RE: [Golfing] Collatz Conjecture - by OversouL - 08-22-2015, 09:55 AM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-22-2015, 12:29 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 08:48 PM
RE: [Golfing] Collatz Conjecture - by Jolly - 08-21-2015, 08:57 PM
RE: [Golfing] Collatz Conjecture - by lux - 08-21-2015, 08:58 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 09:04 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-21-2015, 10:55 PM
RE: [Golfing] Collatz Conjecture - by lux - 08-21-2015, 11:14 PM
RE: [Golfing] Collatz Conjecture - by Akane - 08-22-2015, 01:18 AM
RE: [Golfing] Collatz Conjecture - by Shebang - 08-22-2015, 02:42 AM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 02:51 AM
RE: [Golfing] Collatz Conjecture - by Equinox - 08-22-2015, 03:47 AM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 03:50 AM
RE: [Golfing] Collatz Conjecture - by Equinox - 08-22-2015, 03:53 AM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 03:55 AM
RE: [Golfing] Collatz Conjecture - by Equinox - 08-22-2015, 03:58 AM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-22-2015, 08:35 AM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 02:48 PM
RE: [Golfing] Collatz Conjecture - by OversouL - 08-22-2015, 04:16 PM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 03:58 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-22-2015, 04:07 PM
RE: [Golfing] Collatz Conjecture - by lux - 08-22-2015, 04:18 PM
RE: [Golfing] Collatz Conjecture - by OversouL - 08-22-2015, 04:46 PM
RE: [Golfing] Collatz Conjecture - by Eclipse - 08-22-2015, 05:28 PM



Users browsing this thread: 1 Guest(s)