How can I display the prime numbers between 1 and 100 in c#?
Asked
Active
Viewed 2,249 times
-3
-
7My guess is that this is homework... in which case I'm not going to just provide a solution, because that won't help you learn. However, we'll help you get there. How far have you got? What are you finding difficult? – Jon Skeet Sep 22 '09 at 09:24
-
2Sounds like homework - did you give it a try on your own? – tanascius Sep 22 '09 at 09:24
-
292, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 – manji Sep 22 '09 at 09:24
-
2You should at least try to put effort in answering your own question. Also, notice that a question should end with a question mark, and try to provide any additional info that may be needed in order to be answered. – giorgian Sep 22 '09 at 09:27
-
How to Hello World in C# – Isaac Sep 22 '09 at 09:36
-
Poor kid, you have walked straight into this X-), you should have known better... – Adriaan Stander Sep 22 '09 at 09:43
6 Answers
12
This might help:
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
System.Console.WriteLine("2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97");
}
}
}

sth
- 222,467
- 53
- 283
- 367

luckykrrish
- 128
- 6
3
Assuming you wish to find prime numbers, this is a standard question.

Community
- 1
- 1

Eamon Nerbonne
- 47,023
- 20
- 101
- 166
2
Always fun to try to solve things like this as a one-liner... ;)
Enumerable.Range(2, 100).Where(n => Enumerable.Range(2, n - 2).Count(d => n % d == 0) == 0).ToList().ForEach(Console.WriteLine);

Guffa
- 687,336
- 108
- 737
- 1,005
-
3@zzzuperfly: Seriously? Do you really think that he could hand that in as his own code? Well, in the off chance that he does, I wish I could be there to see him try to explain it... – Guffa Sep 22 '09 at 12:43
-
@Guffa: very nice. I'm surprised to see that, for Range(2, 40000), this code is just 2.3 to 4.7 times (depending on the outer for to be i++ or i+= 2) slower than its trivial translation in two for statements, List and all. – giorgian Sep 22 '09 at 17:54
0
I think you should try to implement the sieve of Eratosthenes. There are more complicated sieves available, but thats usually a good version to start with.
Make sure you choose you datatypes correctly, and dont waste space by picking the wrong ones
If the requirement is as loose as in your question, you even might get away with a simple print statement that outputs a ready list of prime numbers ;) najmeddine already supplied the requiered information for you...

Heiko Hatzfeld
- 3,197
- 18
- 15
0
I suggest http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes I wrote a method like this years ago in Turbo Pascal 7 (wow i'm that old!)
0
IEnumerable<int> PrimeNumbers(int maxCap)
{
yield return 2;
yield return 3;
for (var i = 4; i <= maxCap; i++)
{
var isPrime = true;
for (var j = 2; j <= i / 2; j++)
{
if (i % j != 0) continue;
isPrime = false;
break;
}
if (isPrime) yield return i;
}
}

Levit Kanner
- 121
- 1
- 8