text stringlengths 1 330k |
|---|
namespace PrimeNumbers |
public static class Program |
static int primesCount = 0; |
public static void Main() |
DateTime startingTime = DateTime.Now; |
DateTime endingTime = DateTime.Now; |
TimeSpan span = endingTime - startingTime; |
Console.WriteLine("span = {0}", span.TotalSeconds); |
public static void RangePrime(int start, int end) |
for (int i = start; i != end+1; i++) |
bool isPrime = IsPrime(i); |
Console.WriteLine("number = {0}", i); |
Console.WriteLine("primes count = {0}",primesCount); |
public static bool IsPrime(int ToCheck) |
if (ToCheck == 2) return true; |
if (ToCheck < 2) return false; |
if (IsOdd(ToCheck)) |
for (int i = 3; i <= (ToCheck / 3); i += 2) |
if (ToCheck % i == 0) return false; |
return true; |
else return false; // even numbers(excluding 2) are composite |
public static bool IsOdd(int ToCheck) |
return ((ToCheck % 2 != 0) ? true : false); |
It takes approximately 82 seconds to find and print prime numbers within a range of 1 to 1,000,000, on my Core 2 Duo laptop with a 2.40 GHz processor. And it found 78,498 prime numbers. |
• 1 |
this is way way too slow. the problem is i <= (ToCheck / 3). it should be i <= (ToCheck / i). with it, it might run in 0.1 seconds instead. – Will Ness Oct 10 '18 at 15:11 |
I always use this method for calculating primes numbers following with the sieve algorithm. |
void primelist() |
for(int i = 4; i < pr; i += 2) mark[ i ] = false; |
for(int i = 3; i < pr; i += 2) mark[ i ] = true; mark[ 2 ] = true; |
for(int i = 3, sq = sqrt( pr ); i < sq; i += 2) |
if(mark[ i ]) |
for(int j = i << 1; j < pr; j += i) mark[ j ] = false; |
prime[ 0 ] = 2; ind = 1; |
if(mark[ i ]) ind++; printf("%d\n", ind); |
I don't know about any predefined algorithm but I created my own which is very fast. It can process 20 digits numbers in less than 1 seconds. The max capability of this program is 18446744073709551615. The program is : |
#include <iostream> |
#include <cmath> |
#include <stdlib.h> |
using namespace std; |
unsigned long long int num = 0; |
bool prime(){ |
if(num % 2 == 0 || num == 1){ |
return false; |
unsigned long int square_root = sqrt(num); |
for(unsigned long int i = 3;i <= square_root;i += 2){ |
if(num % i == 0){ |
return false; |
return true; |
int main() |
cout << "Enter number : "; |
cin >> num; |
cout<<"The number is a prime number"<<endl<<endl<<endl<<endl; |
cout<<"The number is not a prime number"<<endl<<endl<<endl<<endl; |
return 0; |
long long unsigned x,y,b,z,e,r,c; |
if(x<2)return 0; |
if(y<x)return 0; |
printf("|\n%llu outputs...\n",r); |
• r is used prior to be initialized – zumalifeguard Dec 14 '11 at 3:44 |
#include <iostream> |
using namespace std; |
int set [1000000]; |
int main (){ |
set [i] = 0; |
int set_size= 1000; |
set [set_size]; |
set [0] = 2; |
set [1] = 3; |
int Ps = 0; |
int last = 2; |
cout << 2 << " " << 3 << " "; |
for (int n=1; n<10000; n++){ |
int t = 0; |
Ps = (n%2)+1+(3*n); |
for (int i=0; i==i; i++){ |
if (set [i] == 0) break; |
if (Ps%set[i]==0){ |
if (t==0){ |
cout << Ps << " "; |
set [last] = Ps; |
//cout << last << endl; |
cout << endl; |
system ("pause"); |
return 0; |
• 12 |
this should be an answer on "How to write unstructured code without actually using GOTO". All this confuscation just to code a simple trial division! (n%2)+1+(3*n) is kind of nice though. :) – Will Ness Mar 4 '12 at 21:25 |
• 1 |
@Will Ness I would've downvoted this as an answer to that question; why use a for loop when a macro will do? :) – Robert Grant Feb 11 '14 at 10:54 |
I know it's somewhat later, but this could be useful to people arriving here from searches. Anyway, here's some JavaScript that relies on the fact that only prime factors need to be tested, so the earlier primes generated by the code are re-used as test factors for later ones. Of course, all even and mod 5 values are f... |
var P = [1, 2], j, k, l = 3 |
for (k = 3 ; k < 10000000 ; k += 2) |
loop: if (++l < 5) |
for (j = 2 ; P[j] <= Math.sqrt(k) ; ++j) |
if (k % P[j] == 0) break loop |
P[P.length] = k |
else l = 0 |
• 1 |
This will give you lots of troubles if you are generating a big number of primes, and for the comparations, better use P[j]*P[j] <= k, because sqrt is pretty slow – Simon Jun 26 '14 at 17:51 |
using namespace std; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.