RE: [Golfing] Collatz Conjecture 08-22-2015, 09:55 AM
#30
(08-21-2015, 06:58 PM)Shebang Wrote: Well, firstly, that code doesn't work rightIt'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
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
![[Image: dHJ4Beo.gif]](http://i.imgur.com/dHJ4Beo.gif)
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.










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 
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)