Click or drag to resize

MathUtilsFactorNaive Method

A simple naive factoring method that uses division by the sequence 2,3,6n±1 up to the square root

Namespace: Orthogonal.Common.Basic
Assembly: Orthogonal.Common.Basic (in Orthogonal.Common.Basic.dll) Version: 2024-04-15 18:00 GMT+10.f27da1471008deaf16b803c17e24a5955690aef1
Syntax
C#
public static IEnumerable<BigInteger> FactorNaive(
	BigInteger n
)

Parameters

n  BigInteger
The number to factor.

Return Value

IEnumerableBigInteger
An enumerable sequence of factors.
Remarks

This code was created as an experiment in early 2021 while playing around with Carmichael numbers and Fermat's Little Theorem. I was so shocked by the high-speed performance of the BigInteger class that I decided it was worth saving this code as a reference.

As the Blog Post explains, the naïve factoring loop works surprisingly fast up until about 1018 after which it hits the exponential wall and quickly becomes impractical.

See Also