 // Prime.cpp : Defines the entry point for the console application.
// Prime.cpp : Defines the entry point for the console application.
 //
//

 #include "stdafx.h"
#include "stdafx.h"
 #include "stdlib.h"
#include "stdlib.h"
 #include <Windows.h>
#include <Windows.h>
 #include <time.h>
#include <time.h>


 /*------------------------------------------------
/*------------------------------------------------
 *    [+FUNCTION+]
*    [+FUNCTION+]
 *
*
 *        NAME: GetPrimeCount
*        NAME: GetPrimeCount
 *        DESC: To count how many primes to be stored
*        DESC: To count how many primes to be stored
 *        PARA:
*        PARA: 
 *             ulPara: The number you want to find all
*             ulPara: The number you want to find all 
 *                     primes less than
*                     primes less than 
 *        AUTH: lonestep@gmail.com
*        AUTH: lonestep@gmail.com
 *
*
 *-[MODIFICATION LOG(S)]---------------------------
*-[MODIFICATION LOG(S)]---------------------------
 *
*
 */
*/

 ULONG GetPrimeCount( ULONG ulPara )
ULONG GetPrimeCount( ULONG ulPara )
 {
{
 if (ulPara <= 536870911UL)
    if (ulPara <= 536870911UL)
 return 28192752UL;
        return 28192752UL;
 else if (ulPara <= 1073741822UL)
    else if (ulPara <= 1073741822UL)
 return 54400030UL;
        return 54400030UL;
 else if ( ulPara <= 1610612733UL)
    else if ( ulPara <= 1610612733UL)
 return 79952416UL;
        return 79952416UL;
 else if (ulPara <= 2147483644UL)
    else if (ulPara <= 2147483644UL)
 return 105097566UL;
        return 105097566UL;
 else if ( ulPara <= 2684354555UL)
    else if ( ulPara <= 2684354555UL)
 return 129949343UL;
        return 129949343UL;
 else if(ulPara <= 3221225466UL)
    else if(ulPara <= 3221225466UL)
 return 154570518UL;
        return 154570518UL;
 else if(ulPara <= 3758096377UL)
    else if(ulPara <= 3758096377UL)
 return 179006097UL;
        return 179006097UL;
 else if(ulPara <= 4294967295UL)
    else if(ulPara <= 4294967295UL)
 return 189961814UL;
        return 189961814UL;
 
    
 return 0UL; /* Never */
    return 0UL; /* Never */
 }
}

 /*------------------------------------------------
/*------------------------------------------------
 *    [+FUNCTION+]
*    [+FUNCTION+]
 *
*
 *        NAME: IsPrime
*        NAME: IsPrime
 *        DESC: Decide whether ulNumber is a prime,
*        DESC: Decide whether ulNumber is a prime,
 *             pulPrimes is all primes that have
*             pulPrimes is all primes that have 
 *             been found, ulPrimCnt is the prime
*             been found, ulPrimCnt is the prime
 *             count of pulPrimes.
*             count of pulPrimes.
 *        AUTH: lonestep@gmail.com
*        AUTH: lonestep@gmail.com
 *
*
 *-[MODIFICATION LOG(S)]---------------------------
*-[MODIFICATION LOG(S)]---------------------------
 *
*
 */
*/

 BOOL IsPrime(ULONG ulNumber, PULONG pulPrimes, ULONG ulPrimCnt )
BOOL IsPrime(ULONG ulNumber, PULONG pulPrimes, ULONG ulPrimCnt )
 {
{
 ULONG i;
    ULONG i;
 if (ulNumber < 2 || pulPrimes == NULL || ulPrimCnt == 0)
    if (ulNumber < 2 || pulPrimes == NULL || ulPrimCnt == 0)
 {
    {
 return FALSE;
        return FALSE;
 }
    }
 for(i = 0; i < ulPrimCnt; i++)
    for(i = 0; i < ulPrimCnt; i++)
 {
    {
 if ( ulNumber%pulPrimes[i-1] == 0 )
        if ( ulNumber%pulPrimes[i-1] == 0 )
 {
        {
 return FALSE;
            return FALSE;
 }
        }
 }
    }
 return TRUE;
    return TRUE;
 }
}

 /*------------------------------------------------
/*------------------------------------------------
 *    [+FUNCTION+]
*    [+FUNCTION+]
 *
*
 *        NAME:AllPrimes
*        NAME:AllPrimes
 *        DESC:Find all primes that smaller than
*        DESC:Find all primes that smaller than 
 *            ulNumber, if pFile is NOT NULL,
*            ulNumber, if pFile is NOT NULL,
 *            then write the result to pFile
*            then write the result to pFile
 *
*
 *        AUTH: lonestep@gmail.com
*        AUTH: lonestep@gmail.com
 *
*
 *-[MODIFICATION LOG(S)]---------------------------
*-[MODIFICATION LOG(S)]---------------------------
 *
*
 */
*/

 PULONG AllPrimes(ULONG ulNumber, PCHAR pFile = NULL)
PULONG AllPrimes(ULONG ulNumber, PCHAR pFile = NULL)
 {
{
 ULONG  ulCur = 0;
    ULONG  ulCur = 0;
 ULONG  ulPrimeCnt = 1;
    ULONG  ulPrimeCnt = 1;

 if (ulNumber <= 2)
    if (ulNumber <= 2)
 {
    {
 return NULL;
        return NULL;
 }
    }
 ULONG  ulGetCnt = GetPrimeCount(ulNumber);
    ULONG  ulGetCnt = GetPrimeCount(ulNumber);
 PULONG pulPrimes = (PULONG)malloc(ulGetCnt*sizeof(ULONG));
    PULONG pulPrimes = (PULONG)malloc(ulGetCnt*sizeof(ULONG));
 if (pulPrimes == NULL)
    if (pulPrimes == NULL)
 {
    {
 return NULL;
        return NULL;
 }
    }

 pulPrimes[0] = 2UL;
    pulPrimes[0] = 2UL;

 for (ulCur = 3; ulCur <ulNumber; ulCur++)
    for (ulCur = 3; ulCur <ulNumber; ulCur++)
 {
    {
 if (IsPrime(ulCur, pulPrimes, ulPrimeCnt))
        if (IsPrime(ulCur, pulPrimes, ulPrimeCnt))
 {
        {
 pulPrimes[ulPrimeCnt] = ulCur;
            pulPrimes[ulPrimeCnt] = ulCur;
 ulPrimeCnt ++;
            ulPrimeCnt ++;
 }
        }
 }
    }
 memset(&pulPrimes[ulPrimeCnt], 0, ulGetCnt - ulPrimeCnt);
    memset(&pulPrimes[ulPrimeCnt], 0, ulGetCnt - ulPrimeCnt);
 if (pFile != NULL)
    if (pFile != NULL)
 {
    {
 DWORD nRet  =0;
        DWORD nRet  =0;
 DWORD nBytes = 0;
        DWORD nBytes = 0;
 HANDLE hFile = CreateFile(pFile,
        HANDLE hFile = CreateFile(pFile,
 GENERIC_WRITE,
            GENERIC_WRITE,
 0,
            0,
 0,
            0,
 CREATE_ALWAYS,
            CREATE_ALWAYS,
 0,
            0,
 0);
            0);
 if (hFile == INVALID_HANDLE_VALUE)
        if (hFile == INVALID_HANDLE_VALUE)
 {
        {
 printf("Create File Failed:%d \n",GetLastError());
            printf("Create File Failed:%d \n",GetLastError());
 return pulPrimes;
            return pulPrimes;
 }
        }
 nRet = WriteFile(hFile, pulPrimes, ulPrimeCnt, &nBytes, 0);
        nRet = WriteFile(hFile, pulPrimes, ulPrimeCnt, &nBytes, 0);
 if (nRet == 0)
        if (nRet == 0)
 {
        {
 printf("Write File Failed:%d\n",GetLastError());
            printf("Write File Failed:%d\n",GetLastError());
 }
        }
 CloseHandle(hFile);
        CloseHandle(hFile);

 }
    }
 return pulPrimes;
    return pulPrimes;
 }
}


 int main(int argc, char* argv[])
int main(int argc, char* argv[])
 {
{
 UINT time = GetTickCount();
    UINT time = GetTickCount();
 PULONG pPrimes = AllPrimes(100000,"1.txt");
    PULONG pPrimes = AllPrimes(100000,"1.txt");
 if ( pPrimes != NULL)
    if ( pPrimes != NULL)
 {
    {
 UINT nNdx = 0;
        UINT nNdx = 0;
 while (pPrimes[nNdx] != '\0')
        while (pPrimes[nNdx] != '\0')
 {
        {
 printf("%lu\t", pPrimes[nNdx]);
            printf("%lu\t", pPrimes[nNdx]);
 nNdx++;
            nNdx++;
 }
        }
 free(pPrimes);
        free(pPrimes);
 }
    }
 time = GetTickCount() -time;
    time = GetTickCount() -time;
 printf("time:%d ms.\n", time);
    printf("time:%d ms.\n", time);
 system("pause");
    system("pause");
 return 0;
    return 0;
 }
}

