|
-
May 15th, 2013, 05:04 PM
#29
Re: Prime Numbers
I have tweeked the code a little bit more. And i know it could furthter be improved
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
long int i = 1,
c = 3,
n,
s,
j;
int input;
cout << "Enter the Values to be calculated: ";
cin >> input;
ofstream myfile ("PrimeNumbers.doc");
if (myfile.is_open()) {
myfile << 1 << "=\t" << 1 <<"\n\n";
myfile << 2 << "=\t" << 2 <<"\n\n";
while (c <= input) {
n = 0;
i += 2;
s = (long)sqrt((float)i);
for (j = 2; j <= s; j++) {
if (i % j == 0) {
n += 1;
break;
}
}
if (c >= (input-50))
{
myfile << c << "=\t" << i <<"\n\n";
c += 1;
}
else if (n == 0) {
c += 1;
}
}
myfile.close();
} else
cout << "Unable to open file";
return 0;
}
And the question on analysis, well nothing tooo big and nothing too serious. and not really analysis, i might keep the record for future references, i dont really know when i might need them. If i get to learn to do something with it in the future then i guess this should be a good start off for me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|