fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int find_nontrivial_factor(int n)
  5. {
  6. int *primes = (int *) malloc((n + 1) * sizeof(int));
  7. int num_primes = 0;
  8. int m = n;
  9. for (int i = 2; i <= m / i; i++)
  10. {
  11. while (m % i == 0)
  12. {
  13. primes[num_primes++] = i;
  14. m /= i;
  15. }
  16. }
  17. if (m > 1)
  18. {
  19. primes[num_primes++] = m;
  20. }
  21.  
  22. for (int i = 0; i < num_primes; i++)
  23. {
  24. if (primes[i] != 1 && primes[i] != n)
  25. {
  26. int factor = primes[i];
  27. free(primes);
  28. return factor;
  29. }
  30. }
  31. free(primes);
  32. return -1;
  33. }
  34.  
  35. int main()
  36. {
  37. int n;
  38. printf("Nhap gia tri n: ");
  39. scanf("%d", &n);
  40.  
  41. int factor = find_nontrivial_factor(n);
  42. if (factor != -1)
  43. {
  44. printf("Mot thua so khong tam thuong cua %d la %d\n", n, factor);
  45. }
  46. else
  47. {
  48. printf("Khong tim thay thua so khong tam thuong cua %d\n", n);
  49. }
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 5408KB
stdin
Standard input is empty
stdout
Nhap gia tri n: Mot thua so khong tam thuong cua 32764 la 2