素数对

  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. bool check(int x)
  5. {
  6. int t = sqrt(x);
  7. for(int i = 2; i <= t; i++)
  8. {
  9. if( x % i == 0) return 0;
  10. }
  11. return 1;
  12. }
  13. int main() {
  14. int n;
  15. bool flag = 0;
  16. cin >> n;
  17. for(int i = 3; i<= n-2; i++)
  18. {
  19. if(check(i) == 1 && check(i+2) == 1)
  20. printf("%d %d\n", i, i+2);
  21. flag = 1;
  22. }
  23. if(flag == 0) cout << "empty";
  24. return 0;
  25. }

哥德巴赫猜想

  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. bool check(int x)
  5. {
  6. int t = sqrt(x);
  7. for(int i = 2; i <= t; i++)
  8. {
  9. if( x % i == 0) return 0;
  10. }
  11. return 1;
  12. }
  13. int main() {
  14. int n;
  15. cin >> n;
  16. for(int i = 3; i<= n/2; i++)
  17. {
  18. if(check(i) == 1 && check( n - i) == 1)
  19. printf("%d = %d + %d\n", n, i, n-i);
  20. break;
  21. }
  22. cout << n;
  23. return 0;
  24. }
  1. //哥德巴赫猜想拓展
  2. #include <iostream>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. bool check(int x)
  6. {
  7. int t = sqrt(x);
  8. for(int i = 2; i <= t; i++)
  9. {
  10. if( x % i == 0) return 0;
  11. }
  12. return 1;
  13. }
  14. int main() {
  15. int n;
  16. cin >> n;
  17. for(int j = 4; j<=n; j+=2)
  18. {
  19. bool flag = 0;
  20. for(int i = 2; i<= j/2; i++)
  21. {
  22. if(check(i) == 1 && check( j - i) == 1 )
  23. {
  24. printf("%d=%d+%d\n", j, i, j-i);
  25. break;
  26. }
  27. // if(flag == 0 || (flag == 1 && (i < (j-i)) ) )
  28. // {
  29. // printf("%d=%d+%d\n", j, i, j-i);
  30. // flag = 1;
  31. // }
  32. }
  33. }
  34. return 0;
  35. }