나는 소수점 이하를 계산하는 프로그램을 작성하여 CPP를 가르치려고합니다. 저는 Win 8.1 Pro (x64)에서 3 세대 [email protected] (x4) 및 8GB RAM의 랩톱에서 Visual Studio Express 2013을 사용하고 있습니다. 나는 cpp (아마도 명백한)에 익숙하지 않지만이 프로그램이 6657 이상에서 작동하기를 기대하고 있습니다. 어떤 생각이라도 감사 할 것입니다!계산 소수; 프로그램 작업/6657 과거의 충돌;
EDIT : 의도 한대로 작동하고 벡터를 사용하는 새 버전의 프로그램입니다. 당신의 도움을 주셔서 감사합니다!
/*
* Prime finder, starts at 2 works up, was working initially
* to 6658, now can run to 150000(highest tested yet) in ~17m25s.
* May need optimization. Uses vector size to check for nth number.
* Must have #include <vector>
*/
std::vector<int> prime; // required for program to work.
int primes(unsigned y){
using namespace std;
unsigned b = 0;
bool z = true; prime.push_back(2);
for (b = 2; prime.size() <= y; b++){
if (b % 2 == 0 && b < y){ b++; }//catches even numbers.
//finds primes to the nth integer
/*decrementing in this loop produced a strange
*end count, used increments instead counts fixed
*and program completes sooner.
*/
for (unsigned j = 2; j < prime.size(); j++){
//if (j > y){ return 0; }//catches infinite loop
if (prime.at(j) >= (b/2))
//removes unneccessary checking
{break;}
if (b%prime.at(j) == 0)
{z = false; j = prime.size();}
}
if (z == true){ prime.push_back(b); }
z = true;
}
}return 0;
}
배열 인덱스가 범위를 벗어남 : 벡터와 같이 이미 구현 된 데이터 구조를 대신 사용하십시오. – mbx