Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone:)
I am getting Timeout Error Code in this code.Someone can please help me
C++
int plenti(int n)
{
    int sum=0,a;
   for(int i=1;i<=n;i++)
   {
       int sum1=0;
       if(i==1)
       {
           continue;
       }
       for(int j=1;j<i;j++)
       {
            a=0;
           if(i%j==0)
           {
               sum1=sum1+j;
           }
           if(sum1>i)
           {
               a=i;
               sum=sum+a;
               break;
           }
           else
           {
               a=0;
           }
       }
   }
   return sum;
}


What I have tried:

I have tried to implement this code
Posted
Updated 28-Aug-20 22:31pm
v2
Comments
KarstenK 29-Aug-20 4:33am    
tip: your handling of a is improvable

On challenge sites, the easy, straight forward code is NEVER the correct answer.

These are challenges to make you think about different ways of solving problems. Usually, if you have loops inside of loops, you're writing the easiest code to understand, but it also executes the slowest.

Scrap your code and rethink how you approach the problem. What you have written will never run fast enough to pass the test.
 
Share this answer
 
Comments
Patrice T 29-Aug-20 1:00am    
+5
Quote:
Someone can please help me

Help what ?
Quote:
I am getting Timeout Error Code in this code.

This error message is typical from challenges sites, it simply means that you code takes too much time to execute.
You simply forgot to tell which site and which problem you try to solve.
Without those, it is impossible to give any help.

The only thing is that your code is too simplistic and they want you to give a better solution, faster.
[Update]
even if not the solution, I would start by simplifying the code:
C++
int plenti(int n) {
    int sum=0,a;
    for(int i=1;i<=n;i++) {
        int sum1=0;
        if(i==1) {
            continue;
        }
        for(int j=1;j<i;j++) {
            a=0;
            if(i%j==0) {
                sum1=sum1+j;
            }
            if(sum1>i) {
                a=i;
                sum=sum+a+i;
                break;
            }
            else {
                a=0;
            }
        }
    }
    return sum;
}
 
Share this answer
 
v2
If ever you decide to post code again, please have the courtesy to comment it.

A comment looks like this:
//  The following code does blah blah blah...
I glanced at the code but don't have the patience, after dinner with wine, to try to figure out what it's trying to do, which sadly is a prerequisite to being able to help. Sure, I could spend many minutes studying it, but frankly I couldn't be bothered when it's not even commented.

Edit1: Thanks for describing what your code is trying to do. Here are some suggestions:

1. How could you eliminate the if(i == 1) check?
2. If you removed a entirely, how could you write sum = sum + a?
3. If 1 is allowed as a divisor, how can you streamline the initialization of sum1 and the for(int j = 1…) loop?
4. In for(int j = 1, j < i…), how could you reduce the maximum value of j?

Edit2: If you knew the result of i/j, what would be the stopping point of the for(int j…) loop?

There are probably other optimizations that rely on analysis, such as numbers that can be immediately excluded if not divisible by both 2 and 3, but this starts to drag in as much math as software, which may not be the intention.
 
Share this answer
 
v5
Comments
Member 14925633 29-Aug-20 10:19am    
I have to find the sum of all numbers whose divisor sum is greater than that number .
N is the number upto which which i have to find out the sum
Greg Utas 29-Aug-20 10:55am    
Thanks. See suggestions edited into above post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900