using System; using System.Collections; using System.Web.Services;
class PrimeFactorizer {
[WebMethod] public long[] Factorize(long factorizableNum){ ArrayList outList = new ArrayList(); long i = 0; int j; try{ long Check = factorizableNum;
//Go through every possible integer //factor between 2 and factorizableNum / 2. //Thus, for 21, check between 2 and 10. for (i = 2; i < (factorizableNum / 2); i++){ while(Check % i == 0){ outList.Add(i); Check = (Check/i); } } //Double-check to see how many prime factors have been added. //If none, add 1 and the number. j = outList.Count; if (j == 0) { outList.Add(1); outList.Add(factorizableNum); } j = outList.Count;
//Return the results and //create an array to hold them. long[] primeFactor = new long[j]; for (j = 0; j < outList.Count; j++){ //Pass the values one by one, making sure //to convert them to type ulong. primeFactor[j] = Convert.ToInt64(outList[j]); } return primeFactor; } catch (Exception) { return null; } } }
Public Class PrimeFactorizer <WebMethod> _ Public Function Factorize(factorizableNum As Long) As Long() Dim outList As New ArrayList() Dim i As Long = 0 Dim j As Integer Try Dim Check As Long = factorizableNum
'Go through every possible integer 'factor between 2 and factorizableNum / 2. 'Thus, for 21, check between 2 and 10. For i = 2 To CLng(factorizableNum / 2) - 1 While Check Mod i = 0 outList.Add(i) Check = CLng(Check / i) End While