![]() |
|
[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) |
RE: [Golfing] Collatz Conjecture - Shebang - 08-22-2015 (08-22-2015, 09:55 AM)OversouL Wrote: Oh, I divided the odd number by 3. XD 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; ![]() 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. RE: [Golfing] Collatz Conjecture - lux - 08-22-2015 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). @OversouL read up on 'Operator Associativity'. Here's a link to get you started. http://www.maths.cam.ac.uk/undergrad/catam/ccatsl/manual/node38.html RE: [Golfing] Collatz Conjecture - lux - 08-22-2015
RE: [Golfing] Collatz Conjecture - Eclipse - 08-22-2015 (08-22-2015, 03:58 PM)Lux Wrote: This is especially true if you're also a JS developer. RE: [Golfing] Collatz Conjecture - OversouL - 08-22-2015 (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). 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? ![]() (08-22-2015, 02:48 PM)Lux Wrote: Had to make a slight change to join the 69 club. 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. RE: [Golfing] Collatz Conjecture - lux - 08-22-2015 (08-22-2015, 04:07 PM)eclipse Wrote: This is especially true if you're also a JS developer. Okay, kid.
RE: [Golfing] Collatz Conjecture - OversouL - 08-22-2015 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;}RE: [Golfing] Collatz Conjecture - Eclipse - 08-22-2015 (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. Updated. |