Created
July 1, 2026 13:12
-
-
Save webag/0a277360955d67df908d40a5a0db1e2a to your computer and use it in GitHub Desktop.
Наибольший простой делитель числа
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <math.h> | |
| // Является ли число простым | |
| int is_prime(int n) { | |
| if (n < 2) | |
| return 0; | |
| for (int i = 2; i < n; i++) { | |
| int temp = n; | |
| while (temp > 0) | |
| temp -= i; | |
| if (temp == 0) | |
| return 0; | |
| } | |
| return 1; | |
| } | |
| // Находим все делители по порядку, если делитель простой, то записываем его в simpleDiv. В simpleDiv остается наибольший. | |
| int maxPrime(int n) { | |
| if (is_prime(n) == 1){ | |
| return n; | |
| } | |
| int simpleDiv = 0; | |
| for (int i = 2; i < n; i++) { | |
| int temp = n; | |
| while (temp > 0){ | |
| temp -= i; | |
| } | |
| if (temp == 0){ | |
| if (is_prime(i) == 1){ | |
| simpleDiv = i; | |
| } | |
| } | |
| } | |
| if (simpleDiv == 0){ | |
| return 0; | |
| } else { | |
| return simpleDiv; | |
| } | |
| } | |
| int main() { | |
| int a; | |
| char c; | |
| if (scanf("%d%c", &a, &c) != 2 || c != '\n') { | |
| printf("n/a\n"); | |
| } else { | |
| printf("%d\n", maxPrime(fabs(a))); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment