Advertisement
Guest User

Divisor script in C

a guest
May 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // C script for calculating the divisors of a number.
  4. // It turns out calculating the square root was not required -- we can determine if we hit it ourselves. Go to the 28th line to see how.
  5.  
  6. // I wrote this script because I wanted a script that is faster than the Python one.
  7. // Indeed, computing the divisors of 9007199254740992 was about 9x faster (23642ms on Python vs. 2641ms on C).
  8.  
  9. int main(int argc, char** argv) {
  10.     if (argc < 2) { // for some reason, argc is 1 if no arguments are specified.
  11.         printf("Specify the number whose divisors you want to take as the first argument.");
  12.     }
  13.     unsigned long long num = 0;
  14.     // The below "for" loop converts a string into an unsigned long long.
  15.     // Say you input "123".
  16.     // The loop:
  17.     // - takes the character "1" (value 49), subtracts 48, gets 1.
  18.     // - multiplies the number by 10 and adds 1 (result = 1).
  19.     // - takes the character "2" (value 50), subtracts 48, gets 2.
  20.     // - multiplies the number by 10 and adds 2 (result = 12).
  21.     // - takes the character "3" (value 51), subtracts 48, gets 3.
  22.     // - multiplies the number by 10 and adds 3 (result = 123).
  23.     for (int i = 0; argv[1][i]; ++i) {
  24.         num = argv[1][i]-48+num*10;
  25.     }
  26.     // i is an unsigned long because the square root of an unsigned long long will always be an unsigned long.
  27.     for (unsigned long i = 1; num/i>=i; // This is the part that stops at the square root of the number.
  28.                                         // When evaluating a non-square number:
  29.                                         // 23 / 1 = 23
  30.                                         // 23 / 2 = 11.5
  31.                                         // 23 / 3 = 7.(6)
  32.                                         // 23 / 4 = 5.75
  33.                                         // 23 / 5 = 4.6
  34.                                         // When evaluating a square number:
  35.                                         // 25 / 1 = 25
  36.                                         // 25 / 2 = 12.5
  37.                                         // 25 / 3 = 8.(3)
  38.                                         // 25 / 4 = 6.25
  39.                                         // 25 / 5 = 5
  40.                                         // We stop once the result, rounded down, is less than or equal to the divisor (the opposite of it being greater than the divisor).
  41.                                         // In both examples, the square root of the number, rounded down, is the result of the final division rounded down.
  42.                                         ++i) {
  43.         if (num % i == 0) {
  44.             printf("%lu\n",i);
  45.             if (i * i != num) { // To avoid getting a duplicate result.
  46.                 printf("%llu\n",num/i);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement