Extreme Optimization > QuickStart Samples > Prime Numbers QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Prime Numbers QuickStart Sample (C#)

Illustrates working with prime numbers using the NumberTheory class (Extreme.Mathematics.Calculus namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // We use many classes from the Extreme.Mathematics.SpecialFunctions 
    // namespace.
    using Extreme.Mathematics;
    using Extreme.Mathematics.SpecialFunctions;

    /// <summary>
    /// Illustrates working with prime numbers using the 
    /// NumberTheory class in the Extreme.Mathematics.SpecialFunctions 
    /// namespace of the Extreme Optimization Mathematics 
    /// Library for .NET.
    /// </summary>
    class PrimeNumbers
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // Factoring numbers
            //

            // The Factorize method returns an array containing
            // the prime factors:
            int index;

            int n = 1001110110;
            int[] factors = NumberTheory.Factorize(n);
            Console.Write("{0} = {1}", n, factors[0]);
            for(index = 1; index < factors.Length; index++)
                Console.Write(" * {0}", factors[index]);
            Console.WriteLine();

            // Factors that occur multiple times is repeated as many
            // times as necessary:
            n = 256 * 6157413;
            factors = NumberTheory.Factorize(n);
            Console.Write("{0} = {1}", n, factors[0]);
            for(index = 1; index < factors.Length; index++)
                Console.Write(" * {0}", factors[index]);
            Console.WriteLine();

            // The 64bit version can safely factor numbers up to 48 bits long:
            long n2 = 1296523 * 1177157L;
            long[] factors2 = NumberTheory.Factorize(n2);
            Console.Write("{0} = {1}", n2, factors2[0]);
            for(index = 1; index < factors.Length; index++)
                Console.Write(" * {0}", factors2[index]);
            Console.WriteLine();

            //
            // Prime numbers
            //

            // The IsPrime method verifies if a number is prime or not.
            n = 801853937;
            Console.WriteLine("{0} is prime? {1}!", n, NumberTheory.IsPrime(n));
            n = 801853939;
            Console.WriteLine("{0} is prime? {1}!", n, NumberTheory.IsPrime(n));

            // MextPrime gets the first prime after a specified number. 
            // You can call it repeatedly to get successive primes.
            // Let//s get the 10 smallest primes larger than one billion:
            n = 1000000000;
            for(index = 1; index < 10; index++)
            {
                n = NumberTheory.NextPrime(n);
                Console.Write("{0,16}", n);
            }
            Console.WriteLine();

            // PreviousPrime gets the last prime before a specified number. 
            n = 1000000000;
            for(index = 1; index < 10; index++)
            {
                n = NumberTheory.PreviousPrime(n);
                Console.Write("{0,16}", n);
            }
            Console.WriteLine();

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army