program giving correct answers but other false ones (Collatz)

edited October 2017 in Questions about Code

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(); }

Tagged:

Answers

Sign In or Register to comment.