The following code takes a number k and its distinct factors f and creates a SortedSet containing all of the divisors of that number. It does this by expanding the set of divisors but multiplying each member of the already known divisors by each prime factor and checking to make sure that the input number k mod the resulting number is 0. Doing this a few times gives a complete list of all of the divisors. There are other methods including recursion that could be used but for what I wrote this to do it should be efficient enough. This program was written to handle numbers of the form \( 10^{n} \) which have only two distinct factors (2,5). While it might be able to handle other numbers with a small set of distinct factors I believe that it would quickly become unwieldy when given a large number with a large prime factor list.
Also updates will be sporadic for a while because it’s my first semester at my new university and I’ve got a heavy course load.
#light
open System
open System.Collections
open System.Collections.Generic
let ExpandSet s f max=
let s2 = new SortedSet<int64>()
for i in s do
for j in f do
let n = i*j
if (n <= max && max % n = 0L) then
s2.Add(n) |> ignore
s2.UnionWith(s)
s2.UnionWith(f)
s2
let fac =
let t = new SortedSet<int64>()
t.Add(2L)
t.Add(5L)
t
let DivisorsOf k f =
let mutable divs = new SortedSet<int64>()
divs.Add(1L) |> ignore
let tries = int64(Math.Ceiling(Math.Log(float k)/Math.Log(2.)))
for i in [1L .. tries] do
Console.WriteLine(i) |> ignore
divs <- ExpandSet divs f k
divs
let PrintSet (s:SortedSet<int64>) =
for i in s do
Console.WriteLine(i)
let t = DivisorsOf 1000000000L fac
PrintSet t