Login Register


[Golfing] Collatz Conjecture filter_list
Author
Message
RE: [Golfing] Collatz Conjecture #31
(08-22-2015, 09:55 AM)OversouL Wrote: Oh, I divided the odd number by 3. XD

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

I wonder why n*=3+1 doesn't work, like n=n*3+1 ??? Anyway, it's 114 bytes now.

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

110 bytes after removing some spaces. :3

The reason you can't do n*=3+1 is because the *= assignment operator multiplies the left by everything on the right. Essentially, it's doing n=n*(3+1).

Speaking of shortening, you can change the ternary expression to be part of an assignment. So you can do n=n%2==0?n/2:3*n+1;

Furthermore, you can change the condition for the ternary to be just n%2. This is essentially the same logic I used. The reason for this is that n%2 will always return 0 or 1, where 0 is a "falsy" value and 1 is a "truthy" value. So, you can rearrange the statement to become n=n%2?3*n+1:n/2; Smile

That's a good start, I think. You should also try to use command-line arguments so you don't need that long code to get it from STDIN.
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #32
Had to make a slight change to join the 69 club.

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

(08-22-2015, 12:29 PM)Shebang Wrote: The reason you can't do n*=3+1 is because the *= assignment operator multiplies the left by everything on the right. Essentially, it's doing n=n*(3+1).

Speaking of shortening, you can change the ternary expression to be part of an assignment. So you can do n=n%2==0?n/2:3*n+1;

Furthermore, you can change the condition for the ternary to be just n%2. This is essentially the same logic I used. The reason for this is that n%2 will always return 0 or 1, where 0 is a "falsy" value and 1 is a "truthy" value. So, you can rearrange the statement to become n=n%2?3*n+1:n/2; Smile

That's a good start, I think. You should also try to use command-line arguments so you don't need that long code to get it from STDIN.

@OversouL read up on 'Operator Associativity'. Here's a link to get you started. http://www.maths.cam.ac.uk/undergrad/cat...ode38.html
(This post was last modified: 08-22-2015, 02:50 PM by lux.)

Reply

RE: [Golfing] Collatz Conjecture #33
[Image: collatz_conjecture.png]

[+] 1 user Likes lux's post
Reply

RE: [Golfing] Collatz Conjecture #34
(08-22-2015, 03:58 PM)Lux Wrote: [Image: collatz_conjecture.png]

This is especially true if you're also a JS developer.

Reply

RE: [Golfing] Collatz Conjecture #35
(08-22-2015, 12:29 PM)Shebang Wrote: The reason you can't do n*=3+1 is because the *= assignment operator multiplies the left by everything on the right. Essentially, it's doing n=n*(3+1).

Speaking of shortening, you can change the ternary expression to be part of an assignment. So you can do n=n%2==0?n/2:3*n+1;

Furthermore, you can change the condition for the ternary to be just n%2. This is essentially the same logic I used. The reason for this is that n%2 will always return 0 or 1, where 0 is a "falsy" value and 1 is a "truthy" value. So, you can rearrange the statement to become n=n%2?3*n+1:n/2; Smile

That's a good start, I think. You should also try to use command-line arguments so you don't need that long code to get it from STDIN.

Oh, I see. Those are some neat tricks. Also what do you mean by command-line arguments? I googled it, but won't you still try to declare somethings in main before you can do it? Which just takes more bytes. Unless you're making something long, then I guess it will be better. Or I'm talking of something else? Far from what you meant? Blush

(08-22-2015, 02:48 PM)Lux Wrote: Had to make a slight change to join the 69 club.

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


@OversouL read up on 'Operator Associativity'. Here's a link to get you started. http://www.maths.cam.ac.uk/undergrad/cat...ode38.html

Thanks for letting me know about it. I thought everything was read from left to right.

I should get involved in this more. I learn a lot from you guys.
[Image: dHJ4Beo.gif]
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.

Reply

RE: [Golfing] Collatz Conjecture #36
(08-22-2015, 04:07 PM)eclipse Wrote: This is especially true if you're also a JS developer.

Okay, kid.

[Image: eade37f4a1.jpg]

Reply

RE: [Golfing] Collatz Conjecture #37
I forgot to add that thanks to Shebang I'm now at 105 bytes.
Code:
#include <iostream> int main(){int n;std::cin>>n;while(n>1){n=n%2?3*n+1:n/2;std::cout<<n<<" ";}return 0;}
[Image: dHJ4Beo.gif]
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.

Reply

RE: [Golfing] Collatz Conjecture #38
(08-22-2015, 04:18 PM)Lux Wrote: Okay, kid.

sorry man all hail gitblub

Spoiler:
jk ily rly


(08-22-2015, 04:46 PM)OversouL Wrote: I forgot to add that thanks to Shebang I'm now at 105 bytes.
Code:
#include <iostream> int main(){int n;std::cin>>n;while(n>1){n=n%2?3*n+1:n/2;std::cout<<n<<" ";}return 0;}

Updated.

Reply







Users browsing this thread: 1 Guest(s)