math-suite/Source/Factorizator/FactorizatorAlgorithm.cs

49 lines
1.2 KiB
C#
Raw Permalink Normal View History

2018-02-05 23:24:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Factorizator
{
static class FactorizatorAlgorithm
{
public static bool Abort { get; set; }
public static void Factorize(decimal number, out decimal[] fact, out decimal[] num)
{
Abort = false;
bool negative = (number < 0);
number = Math.Abs(number);
List<decimal> resultFact = new List<decimal>();
List<decimal> resultNum = new List<decimal>();
for (decimal i = 2; i * i <= number && !Abort; i++)
{
while (number % i == 0)
{
resultFact.Add(i);
if (negative) resultNum.Add(-number);
else resultNum.Add(number);
number /= i;
}
}
if (negative) resultNum.Add(-number);
else resultNum.Add(number);
if (number > 1)
{
if (negative) resultNum.Add(-1);
else resultNum.Add(1);
resultFact.Add(number);
}
fact = resultFact.ToArray();
num = resultNum.ToArray();
}
}
}