Prime Numbers in Visual Basic QuickStart Sample
Illustrates working with prime numbers and the IntegerMath class in the Numerics.NET namespace in Visual Basic.
This sample is also available in: C#, F#, IronPython.
Overview
This QuickStart sample demonstrates how to work with prime numbers using the IntegerMath class from Numerics.NET. The sample shows essential operations for working with prime numbers and factorization.
The sample illustrates several key capabilities:
- Factoring integers into their prime components, including handling of repeated factors
- Testing numbers for primality using the IsPrime method
- Finding the next prime number greater than a given value
- Finding the previous prime number less than a given value
- Working with large numbers up to 48 bits in length
The code demonstrates practical applications like:
- Decomposing composite numbers into their prime factors with multiplicity
- Verifying whether large numbers are prime
- Generating sequences of consecutive prime numbers both forward and backward
- Handling edge cases with large integers and repeated factors
All operations use efficient algorithms optimized for performance while maintaining numerical accuracy.
The code
Option Infer On
' We use classes from the Numerics.NET.SpecialFunctions
' namespace.
Imports Numerics.NET
' Illustrates working with prime numbers using the
' IntegerMath class in the Numerics.NET.SpecialFunctions
' namespace of Numerics.NET.
Module PrimeNumbers
Sub Main()
' The license is verified at runtime. We're using
' a 30 day trial key here. For more information, see
' https://numerics.net/trial-key
Numerics.NET.License.Verify("your-trial-key-here")
'
' Factoring numbers
'
' The Factor method returns a sequence of pairs of the prime factors
' and their multiplicity:
Dim index As Integer
Dim n As Integer = 1001110110
Dim factors As Integer() = IntegerMath.Factor(n)
Console.Write("{0} = {1}", n, factors(0))
For index = 1 To factors.Length - 1
Console.Write(" * {0}", factors(index))
Next
Console.WriteLine()
' Factors that occur multiple times is repeated as many times as necessary:
n = 256 * 6157413
factors = IntegerMath.Factor(n)
Console.Write("{0} = {1}", n, factors(0))
For index = 1 To factors.Length - 1
Console.Write(" * {0}", factors(index))
Next
Console.WriteLine()
' The 64bit version can safely factor numbers up to 48 bits long:
Dim n2 As long = 1296523 * 1177157L
Dim factors2 As long() = IntegerMath.Factor(n2)
Console.Write("{0} = {1}", n2, factors2(0))
For index = 1 To factors2.Length - 1
Console.Write(" * {0}", factors2(index))
Next
Console.WriteLine()
' Let's try a longer number
n2 = 4292017459171704241
factors2 = IntegerMath.Factor(n2)
Console.Write("{0} = {1}", n2, factors2(0))
For index = 1 To factors2.Length - 1
Console.Write(" * {0}", factors2(index))
Next
Console.WriteLine()
'
' Prime numbers
'
' The IsPrime method verifies if a number is prime or not.
n = 801853937
Console.WriteLine("{0} is prime? {1}!", n, IntegerMath.IsPrime(n))
n = 801853939
Console.WriteLine("{0} is prime? {1}!", n, IntegerMath.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:
Console.WriteLine("First 10 primes greater than 1 billion:")
n = 1000000000
For index = 1 To 10
n = IntegerMath.NextPrime(n)
Console.Write("{0,16}", n)
Next
Console.WriteLine()
' PreviousPrime gets the last prime before a specified number.
Console.WriteLine("Last 10 primes less than 1 billion:")
n = 1000000000
For index = 1 To 10
n = IntegerMath.PreviousPrime(n)
Console.Write("{0,16}", n)
Next
Console.WriteLine()
Console.Write("Press Enter key to exit...")
Console.ReadLine()
End Sub
End Module