#c++ help

6 messages · Page 1 of 1 (latest)

naive yew
#

A single natural number n is read from standard input. Among the natural numbers less than n, find the one whose sum of divisors is the largest. In calculating the sum of the divisors of a given number, the number itself should not be taken into account.

int n;
cin>>n;
int max=0;
for (int i=n-1; i>0; i--) {
int sum=0;
if (n%i==0) {
sum+=n;
}
if (max<sum) {
max=sum;
}
}

cout<<max;

Heres my attempt but its apparantley wrong, can someone tell me why it is wrong and how to correct it?

foggy idol
leaden schooner
#
#include <iostream>

int sumOfDivisors(int num) {
    int total = 0;
    for (int i = 1; i < num; i++) {
        if (num % i == 0) {
            total += i;
        }
    }
    return total;
}

int findNumberWithLargestSumOfDivisors(int n) {
    int maxSum = 0;
    int number = 0;
    for (int i = 1; i < n; i++) {
        int currentSum = sumOfDivisors(i);
        if (currentSum > maxSum) {
            maxSum = currentSum;
            number = i;
        }
    }
    return number;
}

int main() {
    int n;
    std::cout << "Enter a natural number: ";
    std::cin >> n;
    int result = findNumberWithLargestSumOfDivisors(n);
    std::cout << "The number less than " << n << " with the largest sum of divisors is: " << result << std::endl;
    return 0;
}
#

here

#

use this

#

i definitely didnt use chat gpt