Thread: Recursive function that returns the sum of all prime numbers from 2-n.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    12

    Recursive function that returns the sum of all prime numbers from 2-n.

    When i use 6 as my n, i get 0, which makes sense to me.;however, why do i get 6 for my final result? Why does it not do 0+5+...etc..?
    Code:
    //source.c file
    #include "Header.h"
    unsigned int sum_primes(unsigned int num)
    {
        //base case
        
        
        if (num == 4 || num == 3)
        {
            return 3;
        }
        if (num%2==0||num%3==0)
        {
            return 0;
        }
        else
        {
            return num+sum_primes(num - 1);
        }
        
    }
    //main
    #include "Header.h"
    
    
    
    
    
    
    
    
    int main() 
    {
        printf("%d\n", sum_primes(6));
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Your code gives zero as a result. What are you talking about?

    Also, that result doesn't make sense at all. The sum of all primes from 2 to 6 should be (2+3+5) = 10.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    Sorry i meant to ask why i was getting zero as my answer. Yea i understand that it should be 10, but where did i go wrong? I understand why i get 0 for my very first num, because 10 is even; however, why does it not call itself again?

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    because 6 is even*

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, it can't work because you immediately return 0 for all multiples of 2 or 3. In my opinion, this function is irredeemable. Recursion and prime numbers don't mix well...
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    oh i see. This is one of my hw assignments and it stated that recursion was mandatory. Imo i think a for loop would make this process much easier, but i can't do that.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Recursion may be mandatory, but is it the only method you're allowed to use? I'm asking because you could easily sneak an 'isPrime(num)' function in there. Like this:
    Code:
    unsigned sum_primes(unsigned num)
    {
        unsigned result = 0;
    
        if (isPrime(num)) {
            result += num;
        }
        if (num >= 2) {
            result += sum_primes(num - 1);
        }
    
        return result;
    }
    There are of course many ways to make this function better, I leave this up to you.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    Ah i see. Thank you very much for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-19-2015, 01:18 PM
  2. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  3. Recursive function: add all even numbers from n to 2
    By Cancino Mel in forum C Programming
    Replies: 11
    Last Post: 05-05-2014, 01:35 AM
  4. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  5. Replies: 5
    Last Post: 05-14-2011, 09:27 PM

Tags for this Thread