下面是一段在屏幕上打印出1-N之间是素数的程序,结果出来有错误,为什么?
#include <stdio.h>
#include <iostream.h>
#include <malloc.h>
int main()
{
int TotalNumber;
//int PrimeNumber;
scanf ("%d",&TotalNumber);
int PrimecountIni = 0;
for (int i = 1 ; i <= TotalNumber; i++)
{
int DivisorNumber=0;
for (int j = 1; j <= i;j++ )
{
if (i%j == 0)
{
DivisorNumber++;
}
}
if (DivisorNumber == 2)
{
PrimecountIni++;
}
}
int Primecount = PrimecountIni +1;
int *PrimeList = (int*)malloc(sizeof(int)*Primecount) ;
PrimeList[0] = 1;
for (int I = 1 ; I <= TotalNumber; I++)
{
int DivisorNumber2=0;
for (int J = 1; J <= I;J++ )
{
if (I%J == 0)
{
DivisorNumber2++;
}
}
if (DivisorNumber2 == 2)
{
PrimeList[I-1] = I;
}
}
for (int a=0;a< Primecount;a++ )
{
printf ("%d",PrimeList[a]);
}
return 0;
}