Guest User

amicable.c

a guest
Jan 23rd, 2017
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. int32_t d(const int32_t n) {
  5.     int32_t result = 1;
  6.     for (int32_t m = 2; m < n / 2 + 1; m++) {
  7.         if ((n % m) == 0) {
  8.             result += m;
  9.         }
  10.     }
  11.     return result;
  12. }
  13.  
  14. int32_t amicable(const int32_t n) {
  15.     const int32_t dn = d(n);
  16.     const int32_t ddn = d(dn);
  17.     int32_t result = 0;
  18.     if (ddn == n && n != dn && n < 100000) {
  19.         result += n;
  20.     }
  21.     return result;
  22. }
  23.  
  24. int main() {
  25.     int32_t result = 0;
  26.     for (int32_t i = 1; i < 10000; i++) {
  27.         result += amicable(i);
  28.     }
  29.     printf("%d\n", result);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment