Untitled

mail@pastecode.io avatar
unknown
c_cpp
6 months ago
1.5 kB
6
Indexable
Never
#include <stdio.h>

int isLastDivisor(int num, int x) {
    int last_divisor_of_num = -1;
    for (int i = num / 2; i > 1; i--){
        if (num % i == 0){
            last_divisor_of_num = i;
            break;
        }
    }
    return (x == last_divisor_of_num) ? 1 : 0;
}

int main() {
    int maxNum = 0;
    int maxSum = 0;



    for (int num = 180; num <= 210; num++) {
        int divisors_sum = 0;
        printf("%d  ", num);
        int divisorCount = 0;

        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                divisors_sum += i;
                printf("%3d", i);
                divisorCount++;

                if (isLastDivisor(num, i) == 0 && i != num / 2) {
                    printf(",");
                    if (divisorCount == 4) {
                        printf("\n     ");
                        divisorCount = 0;
                    }
                }
            }
        }

        if (divisors_sum > maxSum) {
            maxSum = divisors_sum;
            maxNum = num;
        }

        for (int k = 0; k < 4 - divisorCount; k++) {
            printf("   ");
        }
        if (divisorCount == 1 || divisorCount == 0){
            printf("   ");
        } else {
            for (int k = 0; k < 4 - divisorCount; k++) {
                printf(" ");
            }
        }
        printf("   ");
        printf("%3d\n", divisors_sum);
    }

    printf("\n%d\n", maxNum);

    return 0;
}