Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- using namespace std;
- int main() {
- ifstream trial;
- trial.open("C:\Users\User\Documents\trial.txt");
- int prime;
- trial >> prime;
- ofstream write;
- write.open("C:\Users\User\Documents\answer.txt");
- int num[100], b, c, e;
- bool check;
- b = 0;
- switch (prime) {
- case 1:
- {
- write << 2 << endl;
- break;
- }
- case 2:
- {
- write << 3 << endl;
- break;
- }
- case 3:
- {
- write << 5 << endl;
- break;
- }
- case 4:
- {
- write << 7 << endl;
- break;
- }
- default:
- {
- for (int a = 10; a <= 1000000; a++) {
- check = false;
- if (((a % 2) != 0) && ((a % 3) != 0) && ((a % 5) != 0) && ((a % 7) != 0)) // first filter
- {
- for (int d = 0; d <= b; d++) {
- c = num[d];
- if ((a % c) == 0) {
- check = true; // second filter based on previous recorded primes in array
- break;
- }
- }
- if (!check) {
- e = a;
- if (b <= 100) {
- num[b] = a;
- }
- b = b + 1;
- }
- }
- if ((b) == (prime - 4)) {
- write << e << endl;
- break;
- }
- }
- }
- }
- trial.close();
- write.close();
- return 0;
- }
- for( int d=0; d<=b; d++)
- for( int d=0; d<b; d++)
- int someArray[100];
- someArray[150] = 10;
- #include <vector> // std::vector
- ...
- const int MAX_ITEMS = 100;
- std::vector<int> intVector;
- intVector.reserve(MAX_ITEMS); // allocates all memory up-front
- // add items
- for (int i = 0; i < MAX_ITEMS; i++)
- {
- intVector.push_back(i); // this is how you add a value to a vector;
- }
- // print them
- for (int i = 0; i < intVector.size(); i++)
- {
- int elem = intVector[i]; // this access the item at index 'i'
- printf("element %d is %dn", i, elem);
- }
- #include <iostream>
- #include <list>
- #include <math.h>
- #include <functional>
- #include <algorithm>
- using namespace std;
- class is_multiple : public binary_function<int, int, bool>
- {
- public:
- bool operator()(int value, int test) const
- {
- if(value == test) // do not remove the first value
- return false;
- else
- return (value % test) == 0;
- }
- };
- int main()
- {
- list<int> numbersToTest;
- int input = 500;
- // add all numbers to list
- for(int x = 1; x < input; x++)
- numbersToTest.push_back(x);
- // starting at 2 go through the list and remove all multiples until you reach the squareroot
- // of the last element in the list
- for(list<int>::iterator itr = ++numbersToTest.begin(); *itr < sqrt((float) input); itr++)
- {
- int tmp = *itr;
- numbersToTest.remove_if(bind2nd(is_multiple(), *itr));
- itr = find(numbersToTest.begin(), numbersToTest.end(), tmp); //remove_if invalidates iterator
- // so find it again. kind of ugly
- }
- // output primes
- for(list<int>::iterator itr = numbersToTest.begin(); itr != --numbersToTest.end(); itr++)
- cout << *itr << "t";
- system("PAUSE");
- return 0;
- }
- for (int d = 0; d <= b; d++) {
- c = num[d];
- for(int currentInt=2; currentInt<=1000000; currentInt++)
- {check = false; // Basically the idea for this for loop is to run checks against integers. This is the main for loop in this program. I re initialize check to false ( check is a bool declared above this. )
- for( int arrayPrime=0; arrayPrime<currentPrime; arrayPrime++) // This for loop is used for checking the currentInt against previously found primes which are stored in the num array.
- { c=num[arrayPrime];
- if ((currentInt%c)==0) { check = true;// second filter based on previous recorded primes in array
- break;} // this is the check. I check the number against every stored value in the num array. If it's divisible by any of them, then bool check is set to true.
- if ( currentInt == 2)
- { check = false; } // since i preset num[0] = 2 i make an exception for the number 2.
- if (!check)
- {
- e=a;
- if(currentPrime <= 100){
- num[currentPrime]= currentInt;} // This if uses check to see if the currentInt is a prime.
- currentPrime = currentPrime+1;} // increases the value of currentPrime ( previously b ) by one if !check.
- if(currentPrime==prime)
- {
- write<<e<<endl;
- break;} // if currentPrime == prime then write the currentInt into a txt file and break loop, ending the program.
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- using namespace std;
- int main()
- {
- ifstream trial;
- trial.open("C:\Users\User\Documents\trial.txt");
- int prime, e;
- trial>>prime;
- ofstream write;
- write.open("C:\Users\User\Documents\answer.txt");
- int num[10000], currentPrime, c, primePrint;
- bool check;
- currentPrime=0;
- num[currentPrime] = 2;
- currentPrime=1;
- for(int currentInt=2; currentInt<=1000000; currentInt++)
- {check = false;
- for( int arrayPrime=0; arrayPrime<currentPrime; arrayPrime++)
- { c=num[arrayPrime];
- if ((currentInt%c)==0) { check = true;// second filter based on previous recorded primes in array
- break;}
- }
- if (!check)
- { e=currentInt;
- if( currentInt!= 2 ) {
- num[currentPrime]= currentInt;}
- currentPrime = currentPrime+1;}
- if(currentPrime==prime)
- {
- write<<e<<endl;
- break;}
- }
- trial.close();
- write.close();
- return 0;
- }
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- int main()
- {
- int m;
- int n=0;
- char ch;
- fstream fp;
- cout<<"What prime numbers do you want get within? ";
- if((cin>>m)==0)
- {
- cout<<"Bad input! Please try again!n";
- return 1;
- }
- if(m<2)
- {
- cout<<"There are no prime numbers within "<<m<<endl;
- return 0;
- }
- else if(m==2)
- {
- fp.open("prime.txt",ios::in|ios::out|ios::trunc);//create a file can be writen and read. If the file exist, it will be overwriten.
- fp<<"There are only 1 prime number within 2.n";
- fp<<"2n";
- fp.close();
- cout<<"Congratulations! It has worked out!n";
- return 0;
- }
- else
- {
- int j;
- int sq;
- fp.open("prime.txt",ios::in|ios::out|ios::trunc);
- fp<<"2tt";
- n++;
- for(int i=3;i<=m;i+=2)
- {
- sq=static_cast<int>(sqrt(i))+1;
- fp.seekg(0,ios::beg);
- fp>>j;
- for(;j<sq;)
- {
- if(i%j==0)
- {
- break;
- }
- else
- {
- if((fp>>j)==NULL)
- {
- j=3;
- }
- }
- }
- if(j>=sq)
- {
- fp.seekg(0,ios::end);
- fp<<i<<"tt";
- n++;
- if(n%4==0)
- fp<<'n';
- }
- }
- fp.seekg(0,ios::end);
- fp<<"nThere are "<<n<<" prime number within "<<m<<".n";
- fp.close();
- cout<<"Congratulations! It has worked out!n";
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment