|
|
back to boardWhat's wrong with my code? WA#9 #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; } |
|
|