tags:

views:

319

answers:

3

i want to create a program that write all the primes in a file ( i know that its a popular algorithm "Sieve of Eratosthenes" but m trying to make it by my self ) i m trying to eliminate all the complications for the bites that still have the value 1 then wrote them in a File

#include <iostream>
#include <stdlib.h>  
#include <stdio.h>


void afficher_sur_un_ficher (FILE* ficher , int nb_bit );
char el_mask (int x );

int main()
{
FILE* p_fich ;
char tab[4096] , mask , eli_mask ;
int nb_bit = 0 , x , f ;


for (int i = 0 ; i < 4096 ; i++ )
{
    tab[i] = 0xff ;
}
for (int i = 0 ; i < 4096 ; i++ )
{

    for (mask = 0x01 ; mask != 0 ; mask<<=1 )
    {
        if ((tab[i] & mask) != 0 )
        {
            x = nb_bit ; 
            while (( x > 1 )&&(x < 16384))
            {
                    eli_mask = el_mask(x);
                    f = x / 8 ;
                    tab[f] = tab[f] ^ eli_mask ;
                    x = x + nb_bit ;
            }
            afficher_sur_un_ficher (p_fich , nb_bit ) ;
        }
    nb_bit++ ;
    }
}
system ("PAUSE");
return 0 ;
}
void afficher_sur_un_ficher (FILE* ficher , int nb_bit )
{
ficher = fopen("res.txt","a+");
fprintf (ficher ,"%d \t" , nb_bit);
int static z ;
 z = z+1 ;
if ( z%10 == 0)
   fprintf (ficher , "\n");
fclose(ficher);
}

char el_mask (int x )
{
x = x%8 ;
switch (x)
{
    case 0 :
    x = 0b00000001 ;
    break ;
    case 1 :
    x = 0b00000010 ;
    break ;
    case 2 :
    x = 0b00000100 ;
    break ;
    case 3 :
    x = 0b00001000 ;
    break ;
    case 4 :
    x = 0b00010000 ;
    break ;
    case 5 :
    x = 0b00100000 ;
    break ;
    case 6 :
    x = 0b01000000 ;
    break ;
    case 7 :
    x = 0b10000000 ;
    break ;
}
return x ;
}
+5  A: 

There appears to be a problem in the loop that's trying to clear bits indicating non-primes:

while (( x > 1 )&&(x < 16384))
{
    tab[i] = tab[i] ^ mask ;
    x = x * 2 ;
}

Since i doesn't change in this loop, you essentially toggling the same bit off and on while incrementing x. As well as fixing the index into tab[], you probably want to change the operation from xor (^) to something that will clear the bit unconditionally - once a bit is cleared, you don't want processing that bit again for a different factor to 'reset' the bit. Note that a simple incrementing of i wont do, as the multiples of x in other elements of tab might not be in the same bit offset (indeed, a single element of tab[] might contain several multiples of x).

Even if you fix that problem, I think the loop might not be doing what you expect, since x = x * 2; doesn't walk x through its multiples - you'll end up skipping some non-primes.

Some research on how the 'Sieve of Eratosthenes' works might help.

Michael Burr
hamza
+1  A: 
gnometorule
thanks , i just edit my question u may wanna read it again now thanks again
hamza
+1  A: 

You can simplify your main function a bit:

int main (int argc, char** argv) {
    FILE* p_fich;
    char tab[4096] , mask;
    int nb_bit = 0 , x;

    memset(tab, 0xFF, 4096);
    for (int i = 0; i < 4096; i++) {
        for (mask = 0x01; mask != 0; mask <<= 1) {
            if ((tab[i] & mask) != 0) {
                for (x = nb_bit; (x > 1) && (x < 0x4000), x<<=1)
                    tab[i] ^= mask;
                afficher_sur_un_ficher (p_fich, nb_bit);
            }
            nb_bit++;
        }
    }
    system ("PAUSE");
    return 0;
}

Now, to address your question: Your afficher_sur_un_ficher function will print whatever you pass to it, and that function is called for every loop iteration where ((tab[i] & mask) != 0) is true. Since you initialize every tab[i] byte to 0xFF, masking off any given bit combination will always cause that if statement to evaluate true. You are altering the value of tab[i], but once you do, you no longer use that bit so it's not changing the fact that the if statement will always be taken. This is why you are seeing every entry being logged.

Edit: If you simplify away all the code that does not affect the decision to output or the value to output, you end up with the following:

memset(tab, 0xFF, 4096);
for (int i = 0; i < 4096; i++) {
    for (mask = 0x01; mask != 0; mask <<= 1) {
        afficher_sur_un_ficher (p_fich, nb_bit);
        nb_bit++;
    }
}

As you can see, this code will print an incrementing sequence of integers (0 through (4096*8)-1) and is completely independent of the tab array and of the specific values of i and mask. I think there is something missing in your implementation of the algorithm. Do you have a link to a description of the algorithm you are trying to implement? Or is this an algorithm you developed? (Sorry, I didn't understand your description of your algorithm that you added to the question)

bta
thanks man , all what i have is written in french but i will look for something ( sorry i coudlnt connect for the last days )thanks again
hamza