Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: primenumgen.c
  5. *
  6. * Description:
  7. *
  8. * Version: 1.0
  9. * Created: 28/01/15 17:02:23
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: Olivia Theze (foxiepaws), fox@foxiepa.ws
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18.  
  19.  
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24.  
  25. /*
  26. * === FUNCTION ======================================================================
  27. * Name: delete
  28. * Description:
  29. * =====================================================================================
  30. */
  31. unsigned long long *
  32. delete (unsigned long long* arr, unsigned int ele) {
  33. int len = arr[0];
  34.  
  35. unsigned long long* tmp = calloc((len - 1),sizeof(unsigned long long));
  36.  
  37. tmp[0] = len-1;
  38. int loc = 1;
  39. int oloc = 1;
  40. while ((loc < (len - 1)) && (oloc < len)) {
  41. if (oloc != ele) {
  42. tmp[loc] = arr[oloc];
  43. loc++;
  44. }
  45. oloc++;
  46. }
  47.  
  48.  
  49. free (arr);
  50. return tmp;
  51. }
  52. /* ----- end of function delete ----- */
  53.  
  54.  
  55. int main(int argc, char* *argv) {
  56.  
  57. if (argc < 2) {
  58. printf (
  59. "USAGE %s [num]\n"
  60. "\tnum = number to compute primes to\n",
  61. argv[0]
  62. );
  63. return EXIT_FAILURE;
  64. }
  65.  
  66. unsigned long long* nums;
  67. unsigned int point = 0;
  68. size_t arrlen = atoi(argv[1]);
  69. // init the array
  70. nums = malloc(arrlen * sizeof(unsigned long long));
  71. nums[0] = arrlen;
  72. for (int x = 1; x<arrlen; x++) {
  73. nums[x] = x+1;
  74. }
  75.  
  76.  
  77. do {
  78. for (int x = point + 1; x < nums[0]; x++) {
  79. if (nums[x] % nums[point] == 0) {
  80. nums = delete(nums,x);
  81. }
  82. }
  83. point++;
  84. } while ( point < nums[0] );
  85.  
  86.  
  87. printf("[");
  88. for(int t = 1; t < nums[0] - 1; t++) {
  89. printf("%llu,",nums[t]);
  90. }
  91. printf("%llu]\n",nums[nums[0] - 1]);
  92.  
  93. free(nums);
  94. return EXIT_SUCCESS;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement