*Hi there,
So there is this math conjecture which essentially says you can pick any positive integer, aply two rules and it shall always come to 1. The rules are, if the integer is even, divide it by 2. If it is odd, then multiply it by 3 and add 1. So if you had 5 it'd go: 5-16-8-4-2-1. The conjecture holds to the number 5.
So, I've put together a small code to compute the number of steps necessary to come to 1, given an integer. It works fine but I can't use int variables to really large numbers like 1 000 000 000 000 and larger. I've tried to use floating, but then the conditiondoesn't work because iter isn't an integer.Code:if(iter%2==0)
What can I do to solve this problem? I have to use floats, right?
Thanks for any help.
Code:#include<stdio.h> int main(void){ float i,cont,iter; cont=0; for(i=10000000000;i<=1000000000;i++){ iter=i; do{ if(iter%2==0){ iter/=2; cont++;} else{ iter=iter*3+1; cont++;} }while(iter!=1); printf("Collatz Conjecture - iteration: %f\t\t\tsteps:%f\t\t\t\n",i,cont); cont=0;} return 0; }



1Likes
LinkBack URL
About LinkBacks



