This function computes and returns the number of divisors an integer has. But it is very slow and I believe it can be optimized for speed.
unsigned long long int find_no_of_divisors(unsigned long long int myno,FILE *ifp) { unsigned long long int divsrs = 2; unsigned long long int k = 2; if(myno == 1) { return 1; } if(myno == 2) { return 2; } while(1) { if((myno % k) == 0) { divsrs++; if(divsrs == MY_ULLONG_MAX) { printf("Overflow detected in the calculated value for divisors of a number... Exiting"); fclose(ifp); exit(-1); } } k++; if(k > (myno/2)) { break; } } return divsrs; }
This one computes and returns the sum of first n integers:
unsigned long long int next_no(unsigned long long int idx,unsigned long long int cur_no,FILE *ifp) { unsigned long long int next_no; next_no = ((idx)*(idx + 1))/2; if((next_no - idx) != cur_no) { printf("Overflow detected in the value of the calculated number... Exiting"); fclose(ifp); exit(-1); } return next_no; }
Could there be any glitches or functionality issues? (PS - I did not want to use a sqrt()
function due to my portability constraints.)
next_no
with the current index and the result from the last call ofnext_no
. So you're basically calculating the sum from 0 toidx
for eachidx
from 0 toN
. However for now that's just me guessing, so you should be more specific about that. \$\endgroup\$