How to optimize for loop in c by using square root (perfect, abundant, deficient) -


note: left out irrelevant code

so working on ccc 1996 p1, , whole purpose of problem able compute whether integer input perfect, deficient, or abundant number. code have listed above works however, think slow. code, iterates through every number in order find perfect divisors, think inefficient. anyway, have been thinking awhile, cant seem come ways optimize code.

i read online replace < n, < sqrt(n) , switch line in score added s += + (n/i), or of sorts, however, doesn't seem work me. suggestions on more efficient code , decrease run time, because currently, program runs long before reaching output. appreciated thanks!

also, number defined perfect if sum of perfect divisors equal number. number defined abundant if sum of perfect divisors > number. number defined deficient if sum of perfect divisors < number. number not count perfect divisor.

i not familiar big-o notation.

also, number defined perfect if sum of perfect divisors equal number. number defined abundant if sum of perfect divisors > number. number defined deficient if sum of perfect divisors < number. number not count perfect divisor.

you should replace

score += + ((sqrt(numinput))/i); 

with

result = numinput/i; score += (result == || == 1)? : + result; 

the idea each factor less square root, result larger or equal square root, , factor.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -