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





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)