I need help with prime numbers.
-
I have this code, but I need to find out how to make it so the program will check if its a prime number or not. For Example: If user types Number Is prime? 1 No – by definition 2 Yes – the only even prime! ? 3 Yes 4 No 5 Yes 7 Yes 17 Yes 18 No 629 ( = 17 * 37) No static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any number/integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { current = true; break; } } if (current == false) Console.Write("{0} ", j); else current = false; } Console.ReadLine(); } } }
-
I have this code, but I need to find out how to make it so the program will check if its a prime number or not. For Example: If user types Number Is prime? 1 No – by definition 2 Yes – the only even prime! ? 3 Yes 4 No 5 Yes 7 Yes 17 Yes 18 No 629 ( = 17 * 37) No static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any number/integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { current = true; break; } } if (current == false) Console.Write("{0} ", j); else current = false; } Console.ReadLine(); } } }
Where are you stuck? People won't do your homework for you.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
-
I have this code, but I need to find out how to make it so the program will check if its a prime number or not. For Example: If user types Number Is prime? 1 No – by definition 2 Yes – the only even prime! ? 3 Yes 4 No 5 Yes 7 Yes 17 Yes 18 No 629 ( = 17 * 37) No static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any number/integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { current = true; break; } } if (current == false) Console.Write("{0} ", j); else current = false; } Console.ReadLine(); } } }
There's something pretty screwy going on in that code. You can adapt this to suit your needs...
static void Main(string\[\] args) { Console.Write("Enter any integer: "); int inputValue; int.TryParse(Console.ReadLine(), out inputValue); if (isPrimeNumber(inputValue)) { Console.WriteLine(inputValue + " is a prime number."); } else { Console.WriteLine(inputValue + " is not a prime number."); } Console.Write("Any key to exit... "); Console.ReadKey(); } public static bool isPrimeNumber(int num) { if (num < 2) { return false; } for (int i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; }
Paul... I'm not doing his homework - he was nearly there by himself (unless that was a non working example that he was given to fix)
-
There's something pretty screwy going on in that code. You can adapt this to suit your needs...
static void Main(string\[\] args) { Console.Write("Enter any integer: "); int inputValue; int.TryParse(Console.ReadLine(), out inputValue); if (isPrimeNumber(inputValue)) { Console.WriteLine(inputValue + " is a prime number."); } else { Console.WriteLine(inputValue + " is not a prime number."); } Console.Write("Any key to exit... "); Console.ReadKey(); } public static bool isPrimeNumber(int num) { if (num < 2) { return false; } for (int i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; }
Paul... I'm not doing his homework - he was nearly there by himself (unless that was a non working example that he was given to fix)
DaveyM69 wrote:
Paul... I'm not doing his homework
No worries. Your code looks like something the OP can work off of and get some well valued learning experience out of :-D
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
-
I have this code, but I need to find out how to make it so the program will check if its a prime number or not. For Example: If user types Number Is prime? 1 No – by definition 2 Yes – the only even prime! ? 3 Yes 4 No 5 Yes 7 Yes 17 Yes 18 No 629 ( = 17 * 37) No static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any number/integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { current = true; break; } } if (current == false) Console.Write("{0} ", j); else current = false; } Console.ReadLine(); } } }
I wrote a prime class for myself some time ago - so I just dug this out of my archives. It's not the best algorithm, in terms of speed, however it works. Make sure you understand how it works because when your tutor comes to ask you how it works...
class Prime { public static bool isPrime(double numIn) { double testNum; double testLimit; testLimit = numIn; testNum = 3; if (0 == testLimit % 2) { return false; } while (testLimit > testNum) { if (0 == numIn % testNum) { return false; } testLimit = numIn / testNum; testNum += 2; }//while (testLimit > testNum) return true; }//isPrime }//Prime
You always pass failure on the way to success.
-
There's something pretty screwy going on in that code. You can adapt this to suit your needs...
static void Main(string\[\] args) { Console.Write("Enter any integer: "); int inputValue; int.TryParse(Console.ReadLine(), out inputValue); if (isPrimeNumber(inputValue)) { Console.WriteLine(inputValue + " is a prime number."); } else { Console.WriteLine(inputValue + " is not a prime number."); } Console.Write("Any key to exit... "); Console.ReadKey(); } public static bool isPrimeNumber(int num) { if (num < 2) { return false; } for (int i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; }
Paul... I'm not doing his homework - he was nearly there by himself (unless that was a non working example that he was given to fix)
yep. a major performance improvement can be obtained by replacing
for (int i = 2; i < num; i++)
byif (num==2) return true;
for (int i = 3; i*i <= num; i+=2)i.e. only test odd dividers less/equal the square root of num. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
I wrote a prime class for myself some time ago - so I just dug this out of my archives. It's not the best algorithm, in terms of speed, however it works. Make sure you understand how it works because when your tutor comes to ask you how it works...
class Prime { public static bool isPrime(double numIn) { double testNum; double testLimit; testLimit = numIn; testNum = 3; if (0 == testLimit % 2) { return false; } while (testLimit > testNum) { if (0 == numIn % testNum) { return false; } testLimit = numIn / testNum; testNum += 2; }//while (testLimit > testNum) return true; }//isPrime }//Prime
You always pass failure on the way to success.
why would you use doubles both in the API and inside the method, when the problem at hand is basically an integer one? :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
I have this code, but I need to find out how to make it so the program will check if its a prime number or not. For Example: If user types Number Is prime? 1 No – by definition 2 Yes – the only even prime! ? 3 Yes 4 No 5 Yes 7 Yes 17 Yes 18 No 629 ( = 17 * 37) No static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any number/integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { current = true; break; } } if (current == false) Console.Write("{0} ", j); else current = false; } Console.ReadLine(); } } }
Determining whether or not each number is prime as it's entered seems inefficient. As is holding a huge Sieve that may not get fully used. So what about a more dynamic Sieve that you only extend when a new highest number is entered? Might that work?
-
Determining whether or not each number is prime as it's entered seems inefficient. As is holding a huge Sieve that may not get fully used. So what about a more dynamic Sieve that you only extend when a new highest number is entered? Might that work?
So far the linear sieve has not been mentioned nor shown in code in this thread, and it is not the way I would recommend to have a program determine primality for a specific number. The sieve methods are fine to explain the concept, and to execute by hand (i.e. for small numbers) since no divisions are required at all; successive division attempts are the normal first-order attempt in a program. For large numbers (say above 100 digits) completely different techniques are available and are getting refined all the time to investigate primality; most of them use shortcuts that reduce the computational effort tremendously at the expense of a little uncertainty (false positives for some tests, false negatives for other tests). And special libraries ("big integer" or "multiple precision" are used to operate on those numbers). :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
There's something pretty screwy going on in that code. You can adapt this to suit your needs...
static void Main(string\[\] args) { Console.Write("Enter any integer: "); int inputValue; int.TryParse(Console.ReadLine(), out inputValue); if (isPrimeNumber(inputValue)) { Console.WriteLine(inputValue + " is a prime number."); } else { Console.WriteLine(inputValue + " is not a prime number."); } Console.Write("Any key to exit... "); Console.ReadKey(); } public static bool isPrimeNumber(int num) { if (num < 2) { return false; } for (int i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; }
Paul... I'm not doing his homework - he was nearly there by himself (unless that was a non working example that he was given to fix)
-
I wrote a prime class for myself some time ago - so I just dug this out of my archives. It's not the best algorithm, in terms of speed, however it works. Make sure you understand how it works because when your tutor comes to ask you how it works...
class Prime { public static bool isPrime(double numIn) { double testNum; double testLimit; testLimit = numIn; testNum = 3; if (0 == testLimit % 2) { return false; } while (testLimit > testNum) { if (0 == numIn % testNum) { return false; } testLimit = numIn / testNum; testNum += 2; }//while (testLimit > testNum) return true; }//isPrime }//Prime
You always pass failure on the way to success.
-
why would you use doubles both in the API and inside the method, when the problem at hand is basically an integer one? :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Luc Pattyn wrote:
why would you use doubles both in the API and inside the method, when the problem at hand is basically an integer one?
Possible reasons: Because it was nearing midnight when I dug out this class that I wrote as I was learning C#. My gerbils wrote the code. Not all primes have yet been discovered - I may actually discover a non whole prime. (Embarassed clearing of throat :-O )
You always pass failure on the way to success.