Print the prime numbers from 0 to 10,000

Im currently trying to create a program that prints the prime numbers from 0 to 10,000 using only for,do while and ifs. I created this program but it doesn't runs

static void Main(string[] args)
    { 
        for (int x = 2; x < 10000; x++)
        { 
            for (int y = 1; y < x; y++)
            { 
                if (x % y != 0)
                {
                    Console.WriteLine(x); 
                }  
            }
            Console.ReadKey();
        }

I don't know where the problem is and also if the for inside resets.

Upvotes: 2

Views: 31304

Answers (6)

Here is my code where you can generate and print the prime numbers between two numbers (in between string_starting_number and string_last_number). The lowest possible value for string_starting_number is 0 and the highest possible value for string_last_number is decimal.MaxValue-1=79228162514264337593543950334 and not 79228162514264337593543950335 because of the decimal_a++ command inside a for loop which will result to an overflow error.

Take note that you should input the values in string type in string_starting_number and in string_last_number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneratingPrimeNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            string string_starting_number = "1"; //input here your choice of starting number
            string string_last_number = "10"; //input here your choice of last number
            decimal decimal_starting_number = Convert.ToDecimal(string_starting_number);
            decimal decimal_last_number = Convert.ToDecimal(string_last_number);
            string primenumbers = "";
            ulong ulong_b;
            ulong ulong_c;
            if (decimal_starting_number <= ulong.MaxValue)
            {
                ulong ulong_starting_number = Convert.ToUInt64(decimal_starting_number);
                ulong ulong_last_number;
                if (decimal_last_number > ulong.MaxValue)
                {
                    ulong_last_number = ulong.MaxValue;
                }
                else
                {
                    ulong_last_number = Convert.ToUInt64(decimal_last_number);
                }
                if (ulong_starting_number == 0 || ulong_starting_number == 1 || ulong_starting_number == 2 || ulong_starting_number == 3)
                {
                    primenumbers = 2 + " " + 3;
                    ulong_starting_number = 5;
                }
                if (ulong_starting_number % 2 == 0)
                {
                    ulong_starting_number++;
                }
                ulong ulong_a;
                for (ulong_a = ulong_starting_number; ulong_a <= ulong_last_number; ulong_a += 2)
                {
                    ulong_b = Convert.ToUInt64(Math.Ceiling(Math.Sqrt(ulong_a)));
                    for (ulong_c = 3; ulong_c <= ulong_b; ulong_c += 2)
                    {
                        if (ulong_a % ulong_c == 0)
                        {
                            goto next_value_of_ulong_a;
                        }
                    }
                    primenumbers = primenumbers + " " + ulong_a;
                    next_value_of_ulong_a:
                    {
                    }
                }
            }
            if (decimal_last_number > ulong.MaxValue)
            {
                string ulong_maximum_value_plus_two = "18446744073709551617";
                if (decimal_starting_number <= ulong.MaxValue)
                {
                    decimal_starting_number = Convert.ToDecimal(ulong_maximum_value_plus_two);
                }
                if (decimal_starting_number % 2 == 0)
                {
                    decimal_starting_number++;
                }
                decimal decimal_a;
                for (decimal_a = decimal_starting_number; decimal_a <= decimal_last_number; decimal_a += 2)
                {
                    ulong_b = Convert.ToUInt64(Math.Ceiling(Math.Sqrt(ulong.MaxValue) * Math.Sqrt(Convert.ToDouble(decimal_a / ulong.MaxValue))));
                    for (ulong_c = 3; ulong_c <= ulong_b; ulong_c += 2)
                    {
                        if (decimal_a % ulong_c == 0)
                        {
                            goto next_value_of_decimal_a;
                        }
                    }
                    primenumbers = primenumbers + " " + decimal_a;
                    next_value_of_decimal_a:
                    {
                    }
                }
            }
            Console.WriteLine(primenumbers);
            Console.ReadKey();
        }
    }
}

Upvotes: 0

Purushoth
Purushoth

Reputation: 69

Here is simple logic to Print Prime No for any upper limit.

Input : 10 Output : 2 , 3 , 5 ,7

namespace PurushLogics
{
    class Purush_PrimeNos
    {
        static void Main()
        {
            //Prime No Program
            bool isPrime = true;
            Console.WriteLine("Enter till which number you would like print Prime Nos\n");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("Prime Numbers : ");
            for (int i = 2; i <= n; i++)
            {
                for (int j = 2; j <= n; j++)
                {

                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }

                }
                if (isPrime)
                {
                    Console.Write("\t" + i);
                }
               isPrime = true;
            }
            Console.ReadKey();
        }
    }      
}

Upvotes: 0

Ankush Madankar
Ankush Madankar

Reputation: 3834

Try this with no bool variable!!!:

static void Main(string[] args)
{
    for (int x = 2; x < 10000; x++)
    {
        int isPrime = 0;
        for (int y = 1; y < x; y++)
        {
            if (x % y == 0)
                isPrime++;

            if(isPrime == 2) break;
        }
        if(isPrime != 2)
           Console.WriteLine(x);

        isPrime = 0;
    }
    Console.ReadKey();
}

Check Console.ReadKey(); it should be after upper for loop, you can even change condition for upper for loot with <= since 10000 also need to check for prime condition.

Upvotes: 5

malkam
malkam

Reputation: 2355

Below is the efficient way to print prime numbers between 0 and 10000

using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Below are prime numbers between 0 and 10000!");
        Console.WriteLine(2);
        for(int i=3;i<=10000;i++)
        {
            bool isPrime=true;
            for(int j=2;j<=Math.Sqrt(i);j++)
            {
                if(i%j==0)
                {
                    isPrime=false;
                    break;
                }
            }
            if(isPrime)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 882626

The first problem is that x % 1 will always be zero, at least for non-zero x. You need to start the test (inner) loop at one and, for efficiency, stop when you've exceeded the square root of the number itself - if n has a factor f where f > sqrt(n), you would have already found the factor n / f.

The second problem is that you will write out a candidate number every time the remainder is non-zero. So, because 15 % 4 is three, it will be output despite the fact that fifteen is very much a non-prime. It will also be output at 15 % 2, 15 % 4, 15 % 6, 15 % 7, and so on.

The normal (naive) algorithm for prime testing is:

# All numbers to test.

foreach number 2..whatever:
    # Assume prime, check all numbers up to squareroot(number).

    isPrime = true
    foreach test 2..infinity until test * test > number:
        # If a multiple, flag as composite and stop inner loop.

        if number % test == 0:
            isPrime = false
            exit foreach
        end
    end

    # If never flagged as composite, output as prime.

    if isPrime:
        output number
end

Upvotes: 2

Seung Hyun Ra
Seung Hyun Ra

Reputation: 21

Is there any reason that you put Console.ReadKey(); inside of loop?

You should put that out of the loop unless press key during loop.

static void Main(string[] args)
{
    for (int x = 2; x < 10000; x++)
    {

        for (int y = 1; y < x; y++)
        {

            if (x % y != 0)
            {
                Console.WriteLine(x);
            }
        }
    }
    Console.ReadKey();
}

And probably that code is just print lots of x. You should to fix it.

Upvotes: 2

Related Questions