Results 1 to 5 of 5

Thread: Problem with C# program which calculates the prime number

  1. #1
    Join Date
    Oct 2010
    Posts
    17

    Problem with C# program which calculates the prime number

    I have build a C# program just now. Basically, the program is just a prime number finder. I took the time on how fast it could calculate the number:

    It calculates the first 100 prime numbers after 10^15 (1,000,000,000,000,000) in 2 minutes.
    It calculates first prime number after 10^18 (1,000,000,000,000,000,000) in 34 seconds.

    The only small problem is that the program share all numbers up to sqrt(s) to find out if there is a prime number.

    Here's the code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace prime
    {
        class Program
        {
            static void Main(string[] args
                /*Here, the program will bring in an array of args
                 * with [0] as the prime number it should start from,
                 *  and [1] that the number of primes for [0] program to find */)
            {
                ulong primeEnter = Convert.ToUInt64(args[0]) - 1; 
                ushort numberOfPrimes = Convert.ToUInt16(args[1]); 
                bool test = false; 
                for (ushort i = 0; i < numberOfPrimes; i++) 
                {
                    while (test == false) 
                    {
                        primeEnter++; 
                        test = FirstPrime(primeEnter); 
                    }
                    Console.WriteLine(primeEnter); 
                    test = false; 
                }
            }
    
            static bool FirstPrime (ulong primeTest)
            {
                double squareRt = Math.Sqrt(primeTest); 
                uint i = 2; 
                bool test = false;
                while (i <= squareRt) 
                {
                    if (primeTest % i == 0) 
                    {
                        i = 4290000000; 
                        test = true;
                    }
                    i++; 
                }
                if (i != 4290000001) 
                {
                    if (test != true) 
                    {
                        return true; 
                    }
                    else
                    {
                        return false; 
                    }
                }
                else
                {
                    return false; 
                }
                
            }
        }
    }
    Is there anyone who can help me on this?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Problem with C# program which calculates the prime number

    Why are you saving square root in a double ? You are doing so many comparisons which is involving this and so it is performing slow. If you have saved the Sqrt(primeTest) value in a uint or long instead of double then you must have saved a lot of time. According to me the running time for the first 100 prime numbers after 10^15 will go down by 72 to 48 seconds if you do so as I said. Calculations on uint or long is a good deal faster than floating point as double. It is part redundant variables and code lines here. For example, when you find a number that is not a prime number, you can not just return false right away? Should disappear 429000000 [01] stuff, test variable disappears and all tests after the loop disappears. I've rename it to IsPrime, since that is what it returns:

    Code:
    static bool IsPrime (this ulong primeTester)
    {
        var squareRt = (uint)Math.Ceiling(Math.Sqrt(primeTester)); 
        uint i = 2;             
        while (i <= squareRt) 
        {
            if (primeTester % i == 0) 
                return false;
                i++; 
        }
        return true; 
    }
    There is some noise in the main method, where +/-1 stuff and they are nested the loops. We can use a couple of Linq/IEnumerable features to roll it out and get it on a bit more functional form. It does not matter either to or from the performance, but the intention of the code comes out better for the reader without charge, but some flow control lapses and comments:

    Code:
    static void Main(string[] args)
    {
        ulong primeEnter = Convert.ToUInt64(args[0]); // start her, ingen -1 her lenger
        ushort numberOfPrimes = Convert.ToUInt16(args[1]);
        var primes = NumbersFrom(primeEnter).Where(x => x.IsPrime());             
        foreach(var prime in primes.Take(numberOfPrimes)) 
            Console.WriteLine(prime);
    }
    private static IEnumerable<ulong> NumbersFrom(ulong start)
    {
        while(start <= ulong.MaxValue)
            yield return start++; 
    }

  3. #3
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Problem with C# program which calculates the prime number

    There is actually no point in testing for prime numbers since they are quiet odd and it gives no observable effect to optimize for it. Although you will have only half of the total numbers, but even to be found in the first iteration of the IsPrime is very difficult. The saving is a good deal of method calls, but those costs vanishingly small compared to the time it takes to check the other numbers. You may be able to see the effect of it if you're running low starting point, many figures and running with debugger attached. Debuggers will prevent JIT compiler in inline/optimize IsPrime and move next () from the iterator block, and a low starting point minimizes the cost of checks on non-even. Then we optimized the effect of the optimization.

  4. #4
    Join Date
    Oct 2010
    Posts
    17

    Re: Problem with C# program which calculates the prime number

    Thanks for your replies but I do not quite understand what you have said. Can you explain this a little better way? I know there will be less numbers when compared to the numbers but that's my prime requirement.

    Code:
    var primes = NumbersFrom(primeEnter).Where(x => x.IsPrime());             
    foreach(var prime in primes.Take(numberOfPrimes))
       Console.WriteLine(prime);

  5. #5
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Problem with C# program which calculates the prime number

    You have made only one Enumerator to pick out the prime numbers, tell it to extract all numbers from 1 to prime center, where the number satisfies IsPrime. Then he uses the Take to bring out as many primes as saying the task from the enumerator.

    x => x.IsPrime ()
    This is a lambda expression. It takes x as parameter and returns whether x is a prime number. The types of data are collected is based on context. It is a short way to write anonymous functions. In previous versions of C# you could write this instead as :

    Code:
    var primes = NumbersFrom(primeEnter).Where(delegate(ulong x) { return x.IsPrime(); });
    Or even worse, this:

    Code:
    private bool IsNumberPrime(ulong x) { return x.IsPrime() }
    ...
    var primes = NumbersFrom(primeEnter).Where(new Funct<ulong,ulong>(IsNumberPrime));

Similar Threads

  1. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 10:24 AM
  2. program to print prime factor of given number
    By teena_pansare in forum Software Development
    Replies: 4
    Last Post: 27-11-2009, 10:21 AM
  3. program to check prime number in java
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 11:02 PM
  4. Prime calculates missing in C++
    By Kelvin Little in forum Software Development
    Replies: 2
    Last Post: 21-11-2008, 06:10 PM
  5. Prime number
    By mad4jack in forum Software Development
    Replies: 3
    Last Post: 03-10-2008, 09:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,918,935.22166 seconds with 17 queries