Thread: if satement doesn't gaurd and yes i definatly have ==

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    if satement doesn't gaurd and yes i definatly have ==

    i have written the following code that is probably full of bugs as i haven't had a chance to test it yet because at compile i get the following warning.

    ||=== Build: Debug in project 5 (compiler: GNU GCC Compiler) ===|
    /home/ben/Documents/project euler/project 5/main.c||In function ‘divide’:|
    /home/ben/Documents/project euler/project 5/main.c|42|warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]|
    /home/ben/Documents/project euler/project 5/main.c|43|note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’|
    ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MILLION 1000000
    
    int divide(int num, int divisor);
    
    int main()
    {
        int result, num_divide, max_divisor;
    
        printf("Please enter the max number to divide by: ");
        scanf(" %d", &max_divisor);
    
        for (num_divide = max_divisor; num_divide < MILLION; num_divide++)
        {
            if((result = divide(num_divide, max_divisor)))
            {
                break;
            }
        }
    
        if (result == -1)
        {
            printf("naughty naughty you entered zero you cant divide by zero!\n");
        }
        else if (result == 1)
        {
            printf("there is no number between 1 and %d that can be divided by all the integers 1 to %d\n", MILLION, max_divisor);
        }
        else
        {
            printf("the smallest number that can be divided by all the integers 1 to %d is %d\n", max_divisor, num_divide);
        }
        return 0;
    }
    
    int divide(int num, int divisor)
    {
        // sanity checks
    
        if (num == 0); //warning here
        {
            return - 1;
        }
    
        if (num % divisor != 0)
        {
            return 0;
        }
        if (divisor == 1)
        {
            return 1;
        }
        divide(num, divisor - 1);
    }
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    Notice the semi-colon on that line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    oops is the polite version of what just went through my mind lol

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i am trying to find the smallest number that is devisable between 1 and n where n is entered by the user project euler asks for n to be 20 im up to 100,000,000 and still cant find a number that is devisable by 1 to 19 let alone a number that is devisable by 1 to 20.

    to count between 20 and 100,000 as i have the for loop set up now takes a fair while when checking if each number is devisable by n (i know it breaks out of the divide function if a number isn't devisable by the current n but even so)
    coop

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    213
    Is recursion required? I haven't checked the project to see.

    If it is, then I have little to offer, but if performance is of concern and recursion isn't required then don't use recursion, it slows this down a lot.

    Recursion is appropriate when there is something for the recursive call to remember and act upon. There's no such thing happening here. The recursion is merely a loop in this example.

    As such, each call of the recursion is a high drain on performance to simply loop from 1 to 20.

    In debug mode I was able to find the value indicated in a matter of a few seconds. I'll give you this one hint, the value is way, way above 1 million.

    Run the test for modulus in a loop, likely a for loop. Don't put the range limit test inside that loop, exclude num == 0 before the loop runs. That test, especially in a recursive configuration, is performed at each divisor test.

    One more hint, there isn't a number divisible by 1 through 19 before you get to the one divisible by 1 through 20.

    Curiously, the result is also divisible by 21 and 22, but not 23.
    Last edited by Niccolo; 06-03-2019 at 06:41 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    696
    The brute force method may require you to search all the way up to 20! (20 factorial), which is 2,432,902,008,176,640,000.

    However, you should be able to simplify the problem by figuring out which factors are actually needed and then multiply them all together. For example, if the user enters 10, then (unless I'm mistaken) you need only the numbers 4, 7, 9, and 10, because if a number is divisible by those four numbers, then it's divisible by all integers between 1 and 10. So multiply 4*7*9*10 = 2520. Now the problem is figuring out which handful of numbers between 1 and n are required.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    and 19 but my code takes longer to work out n = 19 than n = 22 even though its the same answer

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by laserlight View Post
    Notice the semi-colon on that line.
    Coop - Remember the "for" loop in the code I gave you the other day where I asked you to find the error :P
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    that was the second thing that went through my mind the third isn't repeatable in public either

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. switch satement
    By fenx591 in forum C Programming
    Replies: 3
    Last Post: 07-28-2008, 02:08 PM
  4. Replies: 3
    Last Post: 03-07-2003, 09:06 PM

Tags for this Thread