You are on the Home/Other Tutorials/Project Euler/Euler Support page
Web This Site

Euler Support Routines

Many of the Project Euler questions deal with prime numbers, tests of primality, prime factors, and factors of a number.  It behooves one to build a library of functions to address these "core" functions.  My library of functions is below.

The first function returns all the factors of a number including 1 and the number itself.

Function getFactors(X)
    'Returns all the factors of the number X including 1 and the number itself. _
     Please ensure X contains an acceptable number including one of the decimal _
     data type
    Dim Rslt(), I As Variant
    ReDim Rslt(1): Rslt(0) = 1: Rslt(1) = X
    For I = 2 To Int(Sqr(X))
        If Int(X / I) = X / I Then
            ReDim Preserve Rslt(UBound(Rslt) + 2)
            Rslt(UBound(Rslt) - 1) = I
            If I <> X / I Then Rslt(UBound(Rslt)) = X / I Else ReDim Preserve Rslt(UBound(Rslt) - 1)
            End If
        Next I
    getFactors = Rslt
    End Function