Thread: Project Euler

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    Project Euler

    Hi. I am sure y'all are familiar with Project Euler or may have finished it.
    The first problem: find the sum of natural numbers between 0 and 1000 divisible by 3 or 5
    I have attached my attempted solution.
    Help. My codes not running for whatever reason. I need some pointers.
    Thanks,'

    CodeGuru25

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You already have an iterator called "counter", working in a for loop, so ditch the while loop:

    Code:
    while((counter % 3)|| (counter % 5)=  0)
    
    //make it
    
    if ((counter % 3 == 0) || (counter % 5 == 0))
    I'm not saying the while loop might not get the right answer - probably does. Conceptually, it's very wrong, however.

    I've highlighted the other problem with the code. Also, don't "abbreviate" the mod operattion. It needs TWO == 0's.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    for (counter = 0; counter < 1000; counter ++)
    
    {
      while((counter % 3)|| (counter % 5)=  0)
    The inner loop is terribly wrong. It should be an if (counter divides by 3 or counter divides by 5) in the first place:

    Code:
    if (counter % 3 == 0 || counter % 5 == 0)
    And the printf call is missing a comma between arguments.

    There are other problems that might bite you at some time. For example, why is only the last of the three globals initialized to 0? (Luckily for you globals are automatically zero-initialized.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler Problem 14 Segfault
    By vincent01910 in forum C Programming
    Replies: 5
    Last Post: 11-04-2009, 05:56 AM
  2. Project Euler Question
    By Head In Jar Man in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2009, 02:59 PM
  3. Project Euler Solved but want help improving the code Newbie
    By whiterebbit in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2008, 07:00 AM
  4. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM