We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I'm trying to write a Processing solution about problem # 14 of the Euler project about Collatz suites. The program is written in Processing and in other languages which allows to control the results (in particular Python and Haskel) The program gives correct results up to 500000 but the results for the larger values are false, in particular, unfortunately, for the requested value. I do not understand what's going wrong.
Thank you for any assistance ///////////////////////////// my sketch
int N=100;//(the question is N=1000000)
int longueur( int a){ int champion=0; int valeur =a ; while(valeur !=1){ if(valeur%2==0) valeur =valeur/2; else valeur=3*valeur+1; champion++;} return champion; }
int cherche(int juskou){ //
int champion=3, tdv_champion=7; IntDict tab =new IntDict();
tab.set("3",7); tab.set("10",6); tab.set("5",5); tab.set("16",4); tab.set("8",3); tab.set("4",2); tab.set("2",1); tab.set("1",0);
int compteur= 1;
while( compteur<=juskou){
int tdv=0;
int valeur =compteur;
while(valeur >1){
if(tab.hasKey(str(valeur))) {tdv=tdv+tab.get(str(valeur)); valeur=1;} else{
if(valeur%2==0) valeur =valeur/2;else valeur=3*valeur+1; tdv++;}
}
if (tdv>tdv_champion) {champion=compteur ; tdv_champion=tdv;}
if( !tab.hasKey(str(compteur))) tab.set( str(compteur), tdv);
// println("graine : ", compteur, " ;temps de vol ", tdv, " ;champion : ", champion);
compteur+=1 ;}
return champion;
}
void setup(){
println(cherche(N));
exit(); }
Answers
Edit post, highlight code, press ctrl-o to format.
for N = 100 it prints 97, which is 118 steps
for N = 200 it prints 171, which is 124 steps
is there a list of correct solutions?
i'm wondering whether there's some overflow going on with bigger numbers, that you should be using longs instead of ints.
Thank you for your reply. These are the correct answers
int vs long
I do not think so. the values manipulated in the suite of Collatz do not become very large to my knowledge. It is a track to be tried, can be
thanks again
Edit post, highlight code, press ctrl-o to format.
the code you've posted cannot be used as is and if people can't run the code then they can't fix it
@-) PLUS IT HURTS MY EYES
AND MY SOUL =((
links for later...
https://en.wikipedia.org/wiki/Collatz_conjecture
https://oeis.org/A006884
another link: http://www.dcode.fr/collatz-conjecture - online calculator
and that matches your longueur code with 500,000 and 1,000,000
ok, type 113383 into that last link and it'll tell you that
"Highest number reached 2482111348"
and this https://stackoverflow.com/questions/15004944/max-value-of-integer says
"In Java, the integer is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647."
overflow...
Ok Koogs
it is an integer type history. I did not think that so great numbers were involved I take back my program - and a little bit of markdown for the soul of our friends.
thanks again