Answered # how to show prime numbers in c#

__avd 1,826 Discussion Starter 2aliraza Discussion Starter 2aliraza ddanbe 2,503 Momerath 1,327

0

If your application type is **console **then you have to use *Console.WriteLine()* method. If an app is winform then use UI elements (ListBox).

0

consolethen you have to useConsole.WriteLine()method. If an app is winform then use UI elements (ListBox).

plz if u have any demo code plz post here

0

i made this demo but i want without Boolean Condition

plz help me

```
{
int x,y;
bool a = true;
for ( x = 1; x <=20; x++)
{
for ( y = 2; y <= 20; y++)
{
if (x != y && x % y == 0)
{
a = false;
break;
}
}
if (a)
{
Console.WriteLine(x);
}
a = true;
}
Console.Read();
}
```

0

Write an IsPrime function.

also on line 6 you don't have to let go the variable y all the way to 20.

The square root of 20 will do.

See :

```
int x, y;
bool a = true;
int SQRT = (int)Math.Sqrt(20); // SQRT=4
for (x = 1; x <= 20; x++)
{
for (y = 2; y <= SQRT; y++)
{
if (x != y && x % y == 0)
{
a = false;
break;
}
}
if (a)
{
Console.WriteLine(x);
}
a = true;
}
Console.Read();
```

0

Using enumerator

```
using System;
using System.Collections.Generic;
namespace ConsoleApplication1 {
class Program {
static void Main() {
foreach (int p in Primes(2000)) {
Console.WriteLine("{0}", p);
}
Console.ReadLine();
}
static IEnumerable<int> Primes(int limit) {
if (limit > 1) {
yield return 2;
List<int> primes = new List<int>();
int v = 3;
while (v <= limit) {
Boolean prime = true;
foreach (int p in primes) {
if (v % p == 0) {
prime = false;
break;
}
}
if (prime) {
primes.Add(v);
yield return v;
}
v += 2;
}
}
}
}
}
```

You can speed up generation a bit by adding in the Sqrt stuff:

```
using System;
using System.Collections.Generic;
namespace ConsoleApplication1 {
class Program {
static void Main() {
foreach (int p in Primes(200000)) {
Console.WriteLine("{0}", p);
}
Console.ReadLine();
}
static IEnumerable<int> Primes(int limit) {
if (limit > 1) {
yield return 2;
List<int> primes = new List<int>();
int v = 3;
while (v <= limit) {
Boolean prime = true;
int sv = (int)Math.Sqrt(v);
for (int i = 0; i < primes.Count && primes[i] <= sv; i++) {
if (v % primes[i] == 0) {
prime = false;
break;
}
}
if (prime) {
primes.Add(v);
yield return v;
}
v += 2;
}
}
}
}
}
```

But you'll only need that if you are doing really large lists. And if you are doing those, there are better methods of prime generation :)

This question has already been answered. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...