A abnormally working java loop, -


this brute force attempt solve problem, not giving right answer. program runs, not producing desired output. believe logic , program correct.

this problem of famous site (don't want spoiler)

it asks number produces longest collatz chain under 1 million.

class euler {  public static void main (string args[]) {     long len,longlength=0;     for(long =3;i<=1000000;i++)     {         len = euler14.numfucs(i);         system.out.println("ans"+len+"\t"+i);         if(len>longlength)             longlength=len;     }     system.out.println(longlength); }   public static long numfucs(long num) {     long count=1,$test=0;     while(num>1)     {         if(num%2==0)         {             num=num/2;          }         else         {             num=3*num+1;          }         count++;       }     //system.out.println("\tend");     return count;     } } 

well, feel it'd cheating give code right answer, fellow project euler fan. you're outputting length of longest chain, not number obtains it. if want, can show 1 line needs change, but, honestly, simple fix, , challenge yourself.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -