Given the task: In the interval [a,b] output all numbers such that the sum of their divisors, including one, and not including the number itself will be equal to the number itself. Or -1 , if there are no such numbers in the interval.
It is necessary to optimize the search algorithm so that the program does not hang when working with large numbers. Simply so that after 80 000 does not report Time limit exceeded Killed . I have not used many types of variables and algorithms yet, therefore information on this topic is also important.
#include <stdio.h> int main(){ int a, b; scanf("%d %d", &a, &b); int counter = 0; for(int i = a; i <= b; i++) { int sum = 0; for(int j = 1; j < i; j++) { if(i % j == 0) { sum += j; } } if(sum == i) { printf("%d ", i); counter++; } } if(counter == 0) { printf("-1"); } return 0; }