Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =====================================================================================
- *
- * Filename: primenumgen.c
- *
- * Description:
- *
- * Version: 1.0
- * Created: 28/01/15 17:02:23
- * Revision: none
- * Compiler: gcc
- *
- * Author: Olivia Theze (foxiepaws), fox@foxiepa.ws
- * Organization:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- /*
- * === FUNCTION ======================================================================
- * Name: delete
- * Description:
- * =====================================================================================
- */
- unsigned long long *
- delete (unsigned long long* arr, unsigned int ele) {
- int len = arr[0];
- unsigned long long* tmp = calloc((len - 1),sizeof(unsigned long long));
- tmp[0] = len-1;
- int loc = 1;
- int oloc = 1;
- while ((loc < (len - 1)) && (oloc < len)) {
- if (oloc != ele) {
- tmp[loc] = arr[oloc];
- loc++;
- }
- oloc++;
- }
- free (arr);
- return tmp;
- }
- /* ----- end of function delete ----- */
- int main(int argc, char* *argv) {
- if (argc < 2) {
- printf (
- "USAGE %s [num]\n"
- "\tnum = number to compute primes to\n",
- argv[0]
- );
- return EXIT_FAILURE;
- }
- unsigned long long* nums;
- unsigned int point = 0;
- size_t arrlen = atoi(argv[1]);
- // init the array
- nums = malloc(arrlen * sizeof(unsigned long long));
- nums[0] = arrlen;
- for (int x = 1; x<arrlen; x++) {
- nums[x] = x+1;
- }
- do {
- for (int x = point + 1; x < nums[0]; x++) {
- if (nums[x] % nums[point] == 0) {
- nums = delete(nums,x);
- }
- }
- point++;
- } while ( point < nums[0] );
- printf("[");
- for(int t = 1; t < nums[0] - 1; t++) {
- printf("%llu,",nums[t]);
- }
- printf("%llu]\n",nums[nums[0] - 1]);
- free(nums);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement