10 Replies - 516 Views - Last Post: 10 February 2014 - 07:20 PM Rate Topic: -----

#1 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Collatz Conjecture- Project Euler

Posted 10 February 2014 - 05:22 PM

Hello, my name is Tolga. I just googled c programming help, and found your forum. It looks helpful. Hi to all :)/>/> I'm at first year of computer engineering, in Turkey. I started with Pascal, now I am at C. I am doing my best to improve. Watching YouTube tutorials and trying to solve project euler problems.

I am trying to solve this one at the moment : http://projecteuler.net/problem=14

Program is working pretty well as you can see here : http://puu.sh/6RuKa.png

I think, I wrote the code pretty fine. For example if I set limits as 13-100, it gives correct result.
13-100 >> http://puu.sh/6Ruaw.png

As you can see, 97 is the really longest chain starter. But when I put limit as 1000000, it says "longest : 35655" while answer is 837799. I don't really get it.

For example i run it 291-333, http://puu.sh/6RuoG.png It says 291 but as you can see, longest one is 327 with 144.

Is it about my code or Is it something else?

Code here:
#include <stdio.h>
#include <math.h>

int main()
{
    int i,j=13,chaincounter=1,longest=1;
 
 while(j<1000000){
                  i=j;
                  chaincounter=1;                 
                  // Pseudo code:
                  //     while i is not 1:
                  //         i = i divided by 2 when i is even
                  //         i = i multiplied by 3 plus 1 when i is odd
                  //         increment chain counter
                  if(chaincounter > longest){longest=j;}
                  j=j+2;
                  }

printf("longest chain starter is %d",longest);    
getch();
}



Waiting here for your help, thanks from now!

And also, I wonder when will I start making some 2D games or some graphical stuff because I don't understand, I am solving math problems all day and there isn't anything about graphics, I don't know how to go out of MSDOS command console.

This post has been edited by Skydiver: 10 February 2014 - 07:25 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Collatz Conjecture- Project Euler

#2 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 05:42 PM

sorry for second posting but i want to mention, my point is more about understanding the error here, instead of solving this project euler problem, because i can see this error again in future problems.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:03 PM

Why are you incrementing j by 2?
Was This Post Helpful? 0
  • +
  • -

#4 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:18 PM

View PostSkydiver, on 10 February 2014 - 06:03 PM, said:

Why are you incrementing j by 2?


even ones will never be longest because they just get divided by 2 all the time. so i dont even put them in test.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:30 PM

Just wanted to point out that the nice people who provide those problems have gone to a lot of effort to ask you to refrain from discussing solutions to these problems in public fora, and I think we should respect that. As they say, "if you can't solve it, you can't solve it".
I don't mind giving someone a tip about their programming problem, but I don't really want to see DIC coming up on searches for "solution to euler problem NN". (yes, I'm sure that those solutions exist at some end of that search - but that doesn't mean DIC should be a repository for them)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:35 PM

By any chance, are you using Turbo C/C++? If so, the problem is that you are using a compiler that only uses 16 bits for integers and so you are experiencing first hand what happens when you overflow an integer through arithmetic operations.
Was This Post Helpful? 2
  • +
  • -

#7 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:43 PM

View PostSkydiver, on 10 February 2014 - 06:35 PM, said:

By any chance, are you using Turbo C/C++? If so, the problem is that you are using a compiler that only uses 16 bits for integers and so you are experiencing first hand what happens when you overflow an integer through arithmetic operations.

I don't know what is that, I am using Dev-C++, by the way I changed the code a bit.

A friend used same algorithm in lua , and that gives the right result. But this one gives 910107 as answer, instead of 837799
#include <stdio.h>
#include <math.h>

int main()
{
    int i,j=999999,chaincounter=1,longest=1,bigchain=1;
 
 while(j>1){
                  i=j;
                  chaincounter=1;                 
                  // Pseudo code:
                  //     while i is not 1:
                  //         i = i divided by 2 when i is even
                  //         i = i multiplied by 3 plus 1 when i is odd
                  //         increment chain counter
                  if(chaincounter > bigchain){bigchain=chaincounter;longest=j;}
                  j=j-2;
                  }

printf("longest chain starter is %d",longest);    
getch();
}


Was This Post Helpful? 0
  • +
  • -

#8 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:51 PM

View Postjon.kiparsky, on 10 February 2014 - 06:30 PM, said:

Just wanted to point out that the nice people who provide those problems have gone to a lot of effort to ask you to refrain from discussing solutions to these problems in public fora, and I think we should respect that. As they say, "if you can't solve it, you can't solve it".
I don't mind giving someone a tip about their programming problem, but I don't really want to see DIC coming up on searches for "solution to euler problem NN". (yes, I'm sure that those solutions exist at some end of that search - but that doesn't mean DIC should be a repository for them)


I know what you mean but I don't really care about the project euler problem, I already wrote my code but there is something wrong. I gave my algorithm to a friend who knows lua, and he typed my code in lua 1:1 exactly same, and he gets the true result. The title of topic was not the project euler, some moderator or admin changed it, not me.
Was This Post Helpful? 0
  • +
  • -

#9 daktari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 06:58 PM

Yeah I found the problem, it was about C handling big numbers. I just added long long in front of int and I get the right answer. It wasn't about solving the project euler thing. Thank you anyways.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 07:06 PM

View Postdaktari, on 10 February 2014 - 08:51 PM, said:

I know what you mean but I don't really care about the project euler problem, I already wrote my code but there is something wrong. I gave my algorithm to a friend who knows lua, and he typed my code in lua 1:1 exactly same, and he gets the true result. The title of topic was not the project euler, some moderator or admin changed it, not me.



The point, though, is that other people do care about these problems. And the people who took a lot of trouble to write a really incredible problem set do care a lot about these problems, and they asked you not to post your solutions outside of the dedicated forum they've set up for that purpose. So if you're going to make use of the resource that they provide, you really should, out of self-respect if nothing else, observe the terms under which they provide them, which are really quite reasonable.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Collatz Conjecture- Project Euler

Posted 10 February 2014 - 07:20 PM

Jon makes a good point. I'm going to trim out the portions of the code so that the full solution isn't visible, but still demonstrates the integer overflow problem.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1