Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool PrimalityCheck(int num)
  6. {
  7. bool flag=true;
  8. for(int i = 2; i <= num / 2; i++) {
  9. if(num % i == 0) {
  10. flag = false;
  11. break;
  12. }
  13. }
  14. return flag;
  15. }
  16.  
  17. int main(int argc, char** argv)
  18. {
  19. int primeArraySize = 1;
  20. int *primesOfN = (int *)calloc(primeArraySize, sizeof(int));
  21. int n = atoi(argv[1]);
  22. for (int i = n; i >= 1; i--)
  23. {
  24. if (PrimalityCheck(i))
  25. {
  26. primesOfN = (int *)realloc(primesOfN, 1);
  27. primesOfN[primeArraySize-1] = i;
  28. primeArraySize += 1;
  29. }
  30. }
  31. primeArraySize -= 1;
  32. for (int i = 0; i < primeArraySize; i++)
  33. {
  34. cout << primesOfN[i] << " ";
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement