ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1118. Nontrivial Numbers

What's wrong with my code? WA#9
Posted by Artem Bakhretdinov 15 Oct 2023 23:28
#include <iostream>
#include <limits>
#include <cmath>

bool isPrime(int n) {
    if (n <= 1) {
        return false;
    }

    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
}

int lowest_trivia_number (int I, int J) {
    double min_trivia = std::numeric_limits<double>::max();
    int res = 0;
    for (int i = I; i <= J; i++) {
        int sum_of_own_denominators = 1;
        for(int j = 2; j <= std::sqrt(i); j++){
            if(i % j == 0) {
                sum_of_own_denominators += j;
                if(j != i / j) {
                    sum_of_own_denominators += i / j;
                }
            }
        }

        double cur_trivia = sum_of_own_denominators/i;
        if (cur_trivia < min_trivia) {
            min_trivia = cur_trivia;
            res = i;
        }
    }
    return res;
}

int main() {
    int I, J;

    std::cin >> I >> J;
    if(I == 1) {
        std::cout << 1;
        return 0;
    }

    int largestPrime = -1;

    for (int num = J; num >= I; num--) {
        if (isPrime(num)) {
            largestPrime = num;
            break;
        }
    }

    if (largestPrime != -1) {
        std::cout << largestPrime << std::endl;
    } else {
        std::cout << lowest_trivia_number(I, J);
    }

    return 0;
}