Calculate the number of primes between 1 to 1 0 6 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Good approach demonstrated. There are several optimizations that we could make, especially when we apply some number theoretic results.
There's the first minor improvement you can make in your checking for loop. Once you check n %2, if n %2==1 you no longer need to check n%k for any odd k. Likewise, you only need to check n%3 not n%6, or n%9 etc. This implies that you only need to check n%p for primes p.
This observation leads to two methods:
Iterate across all the numbers from 1 to 10^6 and maintain a vector of all the primes you encounter. When checking a new number X, you only need to check possible factors of p from your vector of primes, up to sqrt(X) still.
A Sieve of Eratosthenes which is similar to the first method. You can read the wikipedia article for the pseudocode and more details, but basically you store an array of the numbers from 1 to 1,000,000 and mark numbers as prime starting from the left. Once you mark a number as prime, mark all its multiples as composite. At the end, go through your array and count all those marked as prime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Log in to reply
Another improvement because my Sieve wasn't ideal, you can stop checking primes once you past sqrt(1000000):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
This is great idea!!
This is my solution, and it's almost 2 times faster than yours.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Dude double x not int x ,else first thing your Programme do if pop a error
Log in to reply
Actually we required to check upto floor of square root. That's why i didn't take double.I have edited my solution.
Sieve of Eratosthenes in C++. The running time is below 0.1 sec.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
You can speed this up dramatically by starting t2 at t1+1
Edit: start t2 at t1, not t1+1
Log in to reply
No.... When using Sieve of Eratosthenes, we make a bool array (or a bitset..) and make all numbers primes. Then, t1 starts from 2, so it's prime and every 2*t2 number is not a prime.. And t1 goes to sqrt(1000000).
Log in to reply
Yes I know, you can see my solution above where I start t2 at t1. When t1 is prime, you only need to check multiples of t1 larger than or equal to t1^2, since any composite number smaller than that will have a prime factor smaller than t1 as well, so you already marked it as false. e.g. once you remove all multiples of 2, and you mark 3 as prime, you no longer need to do 2 * 3, you can go straight to 3 * 3.
Certainly not the most efficient way to do this, but this Python solution works, slowly but surely. I got the value 78,498.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
static bool checkPrime(long n) { if (n == 1) return false; if (n == 2 || n == 3) return true; if ((n + 1) % 6 != 0 && ((n - 1) % 6 != 0)) return false;
long i = Convert.ToInt64(Math.Sqrt(n));
for (; i >= 5; i--)
{
if ((i + 1) % 6 != 0 && ((i - 1) % 6 != 0)) continue;
if (n % i == 0)
return false;
}
return true;
}
static void Main(string[] args)
{
int i = 1;
int x=0;
for (i = 1; i <= 1000000; i++)
{
if(checkPrime(i))
{
x++;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This is my solution in visual studio but it takes a few minutes when I just want the amount of prime numbers when I want to also know all the prime numbers it takes at least a hour
Public Class Form1 Dim ln As Integer Dim wtd As Integer Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click For ln = TextBox1.Text To TextBox2.Text If Not ln Mod 2 = 0 Then For wtd = 2 To ln / 2 + 0.5 If ln Mod wtd = 0 Then Exit For ElseIf wtd = ln / 2 + 0.5 Then If CheckBox1.Checked = True Then TextBox1.Text = TextBox1.Text + 1 End If If CheckBox2.Checked = True Then TextBox2.Text = TextBox2.Text & ln & " " End If End If Next End If Next End Sub End Class
Problem Loading...
Note Loading...
Set Loading...
This is my solution. I am looking for more efficient way to solve this problem because my solution takes almost 1.95 sec.