|
Новичок
Регистрация: 18.05.2009
Сообщений: 12
С нами:
8939101
Репутация:
19
|
|
Простые числа
Нужен код с++ для вычисления 100 Простыч чисел с индексами от 10 миллионов (10 000 000) до 1 миллиард (1 000 000 000).
т.е. как пример: простые числа: 2, 3, 5, 7, 11, 13, 17
Число 11 имеет индекс - 5
Гугл дал код:
Код:
/*
-: Copyright © 1999-2003 AY-programs. All Rights Reserved. :-
E-mail: ay@supermail.ru
-------------------------------------------------------------------------------------
Programma poiska prostih chisel.
Po-umolchaniu programma poluchaet dannie iz "input.txt": chislo, do kotorogo
nujno nayti vse prostie chisla. Resultat vivodit v "output.txt". Kluchem -l programma
nachnet rabotat' v rejime polnogo razbora(s klaviaturi poluchaet imena vhodnogo i
vihodnogo fila, po okonchanii raboti vivodit ih na ekran). Cherez komandnuu stroku
ona mojet poluchat' imena vhodnogo i vihodnogo fila.
-------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR_FILE_IN_NAME "input.txt"
#define STR_FILE_OUT_NAME "output.txt"
int MkOutFile(FILE *f_in, FILE *f_out);
bool bAll=false;
int main(int argc, char* argv[])
{
char sFInName[256]=STR_FILE_IN_NAME, sFOutName[256]=STR_FILE_OUT_NAME, *pNothingDo;
FILE *f_in, *f_out;
char cRead;
if(argc>1){
if((argv[1][0]=='-')&&(argv[1][1]=='l')&&(argv[1][2]=='\0')){
bAll=true;
} else {
pNothingDo=strcpy(sFInName,argv[1]);
if(argc>2) pNothingDo=strcpy(sFOutName,argv[2]);
}
}
//----------------begin all---
if(bAll){
printf("Input name of Input File:");
scanf("%s",sFInName);
printf("Input name of Output File:");
scanf("%s",sFOutName);
}
//----------------end all-----
f_in=fopen(sFInName,"r");
f_out=fopen(sFOutName,"w");
if (!f_in){
printf("Error: can't open file with name %s\n", sFInName);
if(bAll) scanf("%c", &cRead);
return -1;
}
if (!f_out){
printf("Error: can't open file with name %s\n", sFOutName);
if(bAll) scanf("%c", &cRead);
return -1;
}
if(MkOutFile(f_in, f_out)>=0){
printf("Program was finished successfully!\n");
} else {
printf("Program was finished unsuccessfully!\n");
}
fclose(f_in);
fclose(f_out);
//----------------begin all---
if(bAll){
double dRead;
f_in=fopen(sFInName,"r");
f_out=fopen(sFOutName,"r");
printf("Input File:\n");
while(fscanf(f_in, "%lg", &dRead)==1){
printf("%lg ",dRead);
}
printf("\n");
printf("Output File:\n");
while(fscanf(f_out, "%lg", &dRead)==1){
printf("%lg ",dRead);
}
printf("\n");
fclose(f_in);
fclose(f_out);
}
if(bAll) scanf("%c", &cRead);
//----------------end all-----
return 0;
}
int MkOutFile(FILE *f_in, FILE *f_out) //osnovnaya funktsiya
{
long int iSimple;
if(fscanf(f_in, "%ld", &iSimple)!=1) return -1;
if(iSimple<=1) return 1;
if (iSimple==2) {
fprintf(f_out,"%d\n",2);
return 1;
}
if (iSimple==3) {
fprintf(f_out,"%ld\n",2);
fprintf(f_out,"%ld\n",3);
return 1;
}
long int *aiSimples = new long int[(iSimple/2)+1];
if(!aiSimples) return -2;
long int iNOS = 2;
long int iCandidat;
aiSimples[0]=2;
aiSimples[1]=3;
fprintf(f_out,"%ld\n%ld\n",2,3);
iCandidat=3;
iCandidat+=2;
while(iCandidat<=iSimple){
long int iNSravn = 1;
bool bYes = true;
while((aiSimples[iNSravn]*aiSimples[iNSravn])<=iCandidat){
if(!(iCandidat%aiSimples[iNSravn])){
bYes=false;
break;
}
iNSravn++;
}
if(bYes){
iNOS++;
aiSimples[iNOS-1]=iCandidat;
fprintf(f_out,"%ld\n",aiSimples[iNOS-1]);
}
iCandidat+=2;
}
delete [] aiSimples;
return 1;
}
/*
-: Copyright © 1999-2003 AY-programs. All Rights Reserved. :-
E-mail: ay@supermail.ru
*/
Программа выдает последнее число - 1 010 065 393
У этого числа индекс немного выше 50 000 000
вычисленные значения:
Код:
10 000 000 179,424,673
20 000 000 373,587,883
30 000 000 573,259,391
40 000 000 776,531,401
50 000 000 982,451,653
|