Microsoft Small Basic

Program Listing: DFQ430
' Prime factors
' Version 0.2
' Copyright © 2017 Nonki Takahashi. The MIT License.

While "True"
invalid = "True"
While invalid
TextWindow.Write("Number? ")
n = TextWindow.Read()
a = Math.Abs(n)
If (n <= 0) Or (Math.Floor(a) <> a) Then
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("Number should be positive integer.")
TextWindow.ForegroundColor = "Gray"
Else
invalid = "False"
EndIf
EndWhile
PrimeFactor()
np = Array.GetItemCount(p)
ip = Array.GetAllIndices(p)
m = 1
For i = 1 To np
If 1 < i Then
TextWindow.Write(",")
EndIf
TextWindow.Write(ip[i])
m = m * Math.Power(ip[i], p[ip[i]])
EndFor
If n = m Then
TextWindow.ForegroundColor = "Blue"
TextWindow.Write(" (")
Else
TextWindow.ForegroundColor = "Red"
TextWindow.Write(" Overflow:(")
EndIf
TextWindow.Write(n + "=")
For i = 1 To np
If 1 < i Then
TextWindow.Write("*")
EndIf
TextWindow.Write(ip[i])
If 1 < p[ip[i]] Then
TextWindow.Write("^" + p[ip[i]])
EndIf
EndFor
TextWindow.WriteLine(")")
TextWindow.ForegroundColor = "Gray"
EndWhile

Sub PrimeFactor
' calculate primes 2..n
' param n - positive integer
' return p[] - prime factors
Stack.PushValue("local", n)
If n = 1 Then
p = "1=1;"
ElseIf 1 < n Then
p = ""
While Math.Remainder(n, 2) = 0
p[2] = p[2] + 1
n = n / 2
EndWhile
For i = 3 To Math.SquareRoot(n) Step 2
While Math.Remainder(n, i) = 0
p[i] = p[i] + 1
n = n / i
EndWhile
EndFor
If 1 < n Then
p[n] = 1
EndIf
EndIf
n = Stack.PopValue("local")
EndSub