Login Register


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

Reply

RE: [Golfing] Collatz Conjecture #2
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
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #3
(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.

Reply

RE: [Golfing] Collatz Conjecture #4
(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
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #5
(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)


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

Reply

RE: [Golfing] Collatz Conjecture #6
(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
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #7
(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
(This post was last modified: 08-21-2015, 04:54 PM by Eclipse.)

Reply

RE: [Golfing] Collatz Conjecture #8
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))

Reply

RE: [Golfing] Collatz Conjecture #9
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.
[Image: dHJ4Beo.gif]
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.

Reply

RE: [Golfing] Collatz Conjecture #10
(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
[Image: CDUAq9d.png]

Reply







Users browsing this thread: