Sinisterly
[Golfing] Collatz Conjecture - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: [Golfing] Collatz Conjecture (/Thread-Golfing-Collatz-Conjecture)

Pages: 1 2 3 4


[Golfing] Collatz Conjecture - Eclipse - 08-21-2015

Since @Jolly mentioned this to me, I've been researching it and it's as interesting as it is fucking baffling.

The process is simple. You take any positive integer. If it's even, you divide it by two. If it's odd, times it by three and add one. Then you repeat the process with the answer.

Eventually, you'll end up with 1. No matter what the input was.

Your program must calculate and print the entire sequence from the input until you reach 1. Shortest program wins. (https://mothereff.in/byte-counter)

I'll post my solution later.

Leaderboard:

1) @Shebang - 45 Bytes
2) @Eclipse - 66 Bytes
3) @"Stocking" - 69 Bytes
4) @lux - 69 Bytes
5) @OversouL - 105 Bytes


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

What exactly do we need to print, if anything?

Anyways, here's a straightforward Python solution (45 Bytes).
Code:
n=input() while~-n:n=[n/2,3*n+1][n%2];print n

You're lucky Stuck has no while-loops ;P


RE: [Golfing] Collatz Conjecture - Eclipse - 08-21-2015

(08-21-2015, 04:26 PM)Shebang Wrote: What exactly do we need to print, if anything?

Anyways, here's a straightforward Python solution (38 Bytes).
Code:
n=input() while n>1:n=[n/2,3*n+1][n%2]

You're lucky Stuck has no while-loops ;P

Oh, I should have clarified. You print the entire sequence from the input until you reach 1.


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

(08-21-2015, 04:27 PM)eclipse Wrote: Oh, I should have clarified. You print the entire sequence from the input until you reach 1.

Alright, I updated my code Smile


RE: [Golfing] Collatz Conjecture - Jolly - 08-21-2015

(08-21-2015, 04:26 PM)Shebang Wrote: What exactly do we need to print, if anything?

Anyways, here's a straightforward Python solution (46 Bytes).
Code:
n=input() while n>1:n=[n/2,3*n+1][n%2];print n

You're lucky Stuck has no while-loops ;P

As someone learning Python, that line of code makes me cry internally, although it is correct and effective. Although I'm confused on how it functions without any if-statements or loops. (Python 3 user)


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

(08-21-2015, 04:37 PM)Jolly Wrote: As someone learning Python, that line of code makes me cry internally, although it is correct and effective. Although I'm confused on how it functions without any if-statements or loops. (Python 3 user)

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


RE: [Golfing] Collatz Conjecture - Eclipse - 08-21-2015

(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 guy...

My solution:

Code:
i=input() while i!=1: if i%2==0:i=i/2;print i else:i=i*3+1

66 Bytes


RE: [Golfing] Collatz Conjecture - lux - 08-21-2015

70 Bytes. Curse you console.log() !

Code:
for(i=process.argv[2];i!=1;(i%2==0)?(i=i/2):(i=i*3+1),console.log(i))



RE: [Golfing] Collatz Conjecture - OversouL - 08-21-2015

Code:
#include <iostream> int main(){int n;std::cin>>n;while(n>1){n%2==0?n/=2:(n/=3)+1;std::cout<<n<<" ";}return 0;}

110 bytes

I just started c++. Tried my best. Notamused If you have suggestions, I'd be glad to hear it.


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

(08-21-2015, 06:23 PM)OversouL Wrote:
Code:
#include <iostream> int main(){int n;std::cin>>n;while(n>1){n%2==0?n/=2:(n/=3)+1;std::cout<<n<<" ";}return 0;}

110 bytes

I just started c++. Tried my best. Notamused If you have suggestions, I'd be glad to hear it.

Well, firstly, that code doesn't work right Tongue It's printing 10 5 1 for 20, when it should be printing 10 5 16 8 4 2 1. Once you fix that I'll show you a couple ways of shortening it Smile