Method that returns count of its positive factors in C


I am trying to do a method that its function named is factor_count that accepts an integer as its parameter and returns a count of its positive factors.

For example, the six factors of 32 are 1, 2, 4, 8, 16, and 32, so the call of my method should return 6.

int factor_count(int number) { int i, count; for (i=1;i<=number;i++){ if(number%1==0){ count = number%i; } } return count; }


% is the modulo operator. It's remainder after a division. If the remainder after division is zero you should increment count. The remainder from dividing by 1 is always zero.

int factor_count(int number) { int i, count = 0; for (i=1; i<=number; i++) /* increment count when the remainder is zero */ if (number%i==0) count++; return count; }


Please remove number%1==0 and replace it with number%i==0 and then count = count + 1 . Initialize count = 0 outside the loop.


The usual solution is to try divisors: 1,2,3,...,sqrt(n).

Each iteration, note the <a href="https://en.wikipedia.org/wiki/Divisor" rel="nofollow">divisor</a>, <a href="https://en.wikipedia.org/wiki/Quotient" rel="nofollow">quotient</a> and <a href="https://en.wikipedia.org/wiki/Remainder" rel="nofollow">remainder</a>.

If the reminder is 0, then the divisor is a factor. If the quotient it is larger than the divisor, then the quotient is a new factor too.

Continue looping while the divisor is less than the quotient.

Iterating up to the sqrt(n) is sufficient and faster than iterating up to n

int factor_count(int number) { if (number <= 0) { return TBD_Code(n); // OP needs to define functionality for these cases. } int count = 0; int quotient; int divisor = 0; do { divisor++; quotient = number/divisor; int remainder = number%divisor; if (remainder == 0) { count++; if (quotient > divisor) count++; } } while (divisor < quotient); return count; } <hr />

Improvement can include reducing the number, each time a divisor is found. The following is not fully tested.

int factor_count2(int number) { if (number <= 0) { return TBD_Code(n); // OP needs to define functionality for these cases. } int count = 0; int quotient; int divisor = 0; do { divisor++; int remainder; do { quotient = number/divisor; remainder = number%divisor; if (remainder == 0) { number = quotient; count++; if (quotient > divisor) count++; else break; } } while (remainder == 0); } while (divisor < quotient); return count; } <hr />

Even more improvements can be had with factor_count2() going to the next prime.

// divisor++; divisor = next_prime(divisor);


  • Multithreaded application, am I doing it right?
  • Python Prime Number Generator
  • Ways to implement recipricals on Verilog
  • How can I multiply and divide 64-bit ints accurately?
  • Three-way xor-like function
  • Resize rectangle in Paper.js
  • Using Checkboxes to contol an Input.value (With an annoying twist.)
  • Dividing a number into m parts uniformly randomly
  • something wrong with my float operation
  • conditional line-by-line reading from file
  • Language Scheme: find the sum of proper divisors
  • Find uninitialized variables in C&C++
  • Fastest way to decode a hexadecimal digit
  • Matrix Circular Shift
  • Does WITH statement execute once per query or once per row?
  • Printing nested dictionary values that may or may not exist
  • Inno Setup Search for specifc file on a CD, retrieve exact filepath and return value to [Files]-Sect
  • finding greatest prime factor using recursion in c
  • ProgressBar Paint Method?
  • C: Custom strlen() library function
  • Distribute Range of Numbers between each threads
  • Perspective projection, 4 points
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • RxJava debounce by arbitrary value
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • How to model a transition system with SPIN
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • How to disable jQuery.jplayer autoplay?
  • Python: how to group similar lists together in a list of lists?
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • java string with new operator and a literal