Lab 2/sieve-heap.c
void print_sieve(int n){
  // int *x
  //mem[x] -> y
    //int* pointer_to_array = (int *) malloc(sizeof(int)*(n-1));
    //bool is 1 byte
    bool* sieves_prime = (bool *) malloc(sizeof(bool)*(n+1)); //sieves_prime[i] == 1 om i är primtal annars == 0 - n+1 eftersom vi har med 0 också därmed blir det n+1 värden
    //example n==5
    //[0,1,2,3,4,5] i.e. sieves_prime[0] <=> is_prime(2)
    //[0,0,0,0,0,0]
    for (int i = 0; i <= 2; i++){//initialize number 0 and 1 to false
       sieves_prime [i] = false; //syntactic sugar for *(sieves_prime+i) = 0;
   }
   for (int i = 2; i <= n; i++){
       sieves_prime [i] = true; // *(sieves_prime+i) = 1;
   }
   
    int sqrt_rounded_up_int = (int) sqrt(n) +1;
   for  (int i = 2; i <= sqrt_rounded_up_int; i++){ 
       if(sieves_prime[i] == 1){ // 1 indicates that its a prime *(sieves_prime+i) == 1;
           for (int j = i*i; j <= n; j+=i){//i^2, i^2+i, i^2+2i...
           sieves_prime[j] = 0; // *(sieves_prime+j) = 0;

           }
        }
    
   }
   int num_prints_since_last_newline = 0;
   for (int i = 0; i <= n; i++){

       if(sieves_prime[i] == 1){
           print_number(i);
           num_prints_since_last_newline +=1;
       }
       if(num_prints_since_last_newline >= COLUMNS){
        printf("\n");
        num_prints_since_last_newline = 0;
       }
    }
    //printf("%8d",&sieves_prime);
    free(sieves_prime);

   }
///does not use more than n bytes of the array (n+12 bytes total)
///@param n - largest number to check for primality / all numbers 2 to n are checked for prime status
///@return an array 
does not use more than n bytes of the array (n+12 bytes total)
does not use more than n bytes of the array ( n + 12 bytes total )
void print_sieve(int n){
    bool* sieves_prime = (bool *) malloc(sizeof(bool)*(n+1)); 
    for (int i = 0; i <= 2; i++){
       sieves_prime [i] = false; 
   }
   for (int i = 2; i <= n; i++){
       sieves_prime [i] = true; 
   }
    int sqrt_rounded_up_int = (int) sqrt(n) +1;
   for  (int i = 2; i <= sqrt_rounded_up_int; i++){ 
       if(sieves_prime[i] == 1){ 
           for (int j = i*i; j <= n; j+=i){
           sieves_prime[j] = 0; 
           }
        }
   }
   int num_prints_since_last_newline = 0;
   for (int i = 0; i <= n; i++){
       if(sieves_prime[i] == 1){
           print_number(i);
           num_prints_since_last_newline +=1;
       }
       if(num_prints_since_last_newline >= COLUMNS){
        printf("\n");
        num_prints_since_last_newline = 0;
       }
    }
    free(sieves_prime);
   }
void print_sieve ( int n ) { bool * sieves_prime = ( bool * ) malloc ( sizeof ( bool ) * ( n + 1 ) ) ; for ( int i = 0 ; i <= 2 ; i ++ ) { sieves_prime [ i ] = false ; } for ( int i = 2 ; i <= n ; i ++ ) { sieves_prime [ i ] = true ; } int sqrt_rounded_up_int = ( int ) sqrt ( n ) + 1 ; for ( int i = 2 ; i <= sqrt_rounded_up_int ; i ++ ) { if ( sieves_prime [ i ] == 1 ) { for ( int j = i * i ; j <= n ; j += i ) { sieves_prime [ j ] = 0 ; } } } int num_prints_since_last_newline = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( sieves_prime [ i ] == 1 ) { print_number ( i ) ; num_prints_since_last_newline += 1 ; } if ( num_prints_since_last_newline >= COLUMNS ) { printf ( " \n " ) ; num_prints_since_last_newline = 0 ; } } free ( sieves_prime ) ; }
does not use more than n bytes of the array (n+12 bytes total)
does not use more than n bytes of the array ( n + 12 bytes total )
// int *x //mem[x] -> y //int* pointer_to_array = (int *) malloc(sizeof(int)*(n-1)); //bool is 1 byte //sieves_prime[i] == 1 om i är primtal annars == 0 - n+1 eftersom vi har med 0 också därmed blir det n+1 värden //example n==5 //[0,1,2,3,4,5] i.e. sieves_prime[0] <=> is_prime(2) //[0,0,0,0,0,0] //initialize number 0 and 1 to false //syntactic sugar for *(sieves_prime+i) = 0; // *(sieves_prime+i) = 1; // 1 indicates that its a prime *(sieves_prime+i) == 1; //i^2, i^2+i, i^2+2i... // *(sieves_prime+j) = 0; //printf("%8d",&sieves_prime);