Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow questionthis Stack Overflow question on how the time complexity was determined.

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

Rollback to Revision 6
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$ \BigO{n + \left( n \times \dfrac{n}{2}} + \left( \dfrac{n}{2} \right) \right) \$\$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ \BigO{n^{2}}\$\$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ \BigO{n \left( \log n \right) \left( \log \log n \right)} \$\$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

\$ \newcommand{\BigO}[1]{\operatorname{O}\left(#1\right)} \$

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$ \BigO{n + \left( n \times \dfrac{n}{2}} + \left( \dfrac{n}{2} \right) \right) \$, or \$ \BigO{n^{2}}\$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ \BigO{n \left( \log n \right) \left( \log \log n \right)} \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

\$ \newcommand{\BigO}[1]{\operatorname{O}\left(#1\right)} \$

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

Just made a new command for the Big O notation
Source Link
syb0rg
  • 21.8k
  • 10
  • 112
  • 191

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$\$ \BigO{n + \left( n \times \dfrac{n}{2}} + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$\$ \BigO{n^{2}}\$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$\$ \BigO{n \left( \log n \right) \left( \log \log n \right)} \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

\$ \newcommand{\BigO}[1]{\operatorname{O}\left(#1\right)} \$

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$O \left( n + \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \right) \$, or \$ O(n^{2}) \$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ O \left(n \left( \log n \right) \left( \log \log n \right) \right) \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

Is there a better way of doing this? If so, how?

Yes, there is. Your current algorithm is \$ \BigO{n + \left( n \times \dfrac{n}{2}} + \left( \dfrac{n}{2} \right) \right) \$, or \$ \BigO{n^{2}}\$. (The first \$n\$ is the iterating of the numbers, the \$ \left( n \times \dfrac{n}{2} \right) + \left( \dfrac{n}{2} \right) \$ is the time complexity of a triangular number.)

If you use the Sieve of Eratosthenes, which works like this...

enter image description here
(image courtesy of linked Wikipedia article)

you get \$ \BigO{n \left( \log n \right) \left( \log \log n \right)} \$, which is faster. See this Stack Overflow question on how the time complexity was determined.

\$ \newcommand{\BigO}[1]{\operatorname{O}\left(#1\right)} \$

added 15 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Use times symbol, not a star
Source Link
syb0rg
  • 21.8k
  • 10
  • 112
  • 191
Loading
Rollback to Revision 2
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading
rollback until fix
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading
Made the LaTeX stuff more pretty.
Source Link
syb0rg
  • 21.8k
  • 10
  • 112
  • 191
Loading
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading