ok basically i created a program to calculate the primes from 0 to a given number up to about 500.000 (not sure)
please give me your opinions about it and perhaps stuff i can improve
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
void c_primes(long param);
void c_primes(long param)
{
float t_before, t_after;
long int arr_primes[param],i,j;
for (i=2;i<=param;i++)
{
arr_primes[i]=i;
}
t_before=GetTickCount();
for (i=2;i<=param;i++)
{
if (arr_primes[i] == 0) { continue; }
for (j=(i+1);j<=param;j++)
{
if (arr_primes[j] != 0 && arr_primes[j]%i==0)
{
arr_primes[j]=0;
}
}
}
t_after=GetTickCount();
cout<<"\n\nCalculation time: "<<t_after-t_before<<" milliseconds\n\n";
cout<<"Press a key to display the prime numbers\n";
system("PAUSE");
cout<<"\nPrimes from 0 to "<<param<<":\n";
for (long x=2;x<param+1;x++)
{
if (arr_primes[x] != 0) { cout<<arr_primes[x]<<", "; }
}
cout<<"\n";
delete [] arr_primes;
}
int main(int argc, char *argv[])
{
long m_primes;
cout<<"\n Welcome\n\Calculate prime numbers from 0 to: ";
cin>>m_primes;
c_primes(m_primes); //c_primes stands for calculate primes
system("PAUSE");
return EXIT_SUCCESS;
}
basic explanation is, it loops through an array of numbers, when it's not a prime it sets the value to 0 (afterwards it prints them)
ofcourse there are many more and also easier ways to do it but this works quite fine
you can also output it all to a file by creating a .bat file (or just write a function which writes the stuff to disk) which contains
@echo off
nameoftheexe.exe >> nameofthetextfile.txt