Код: Выделить всё
static void PrimeFactors(int userInput)
{
// create a variable which is a new version of userInput that can be manipulated by the method
int input = userInput;
// declare a new list which will contain all of the factors of the user input.
var factors = new List();
// While the input is greater than 1, if input mod counter is equal to 0,
// add counter to factor list and set input value to input / counter
// if input % counter != 0, break and start the for loop again
for(int counter = 1; input >= 1; )
{
if(input % counter == 0)
{
factors.Add(counter);
input = input / counter;
counter++;
}
else
{
counter++;
}
}
// display the prime factors
foreach (int factor in factors)
{
Console.Write($"{factor} ");
}
}
Я знаю, что мой код не является полным с точки зрения фильтрации ТОЛЬКО простых чисел, но я хочу устранить эти отрицательные числа, прежде чем уточнять вещи, чтобы в список попадали только простые числа.
Подробнее здесь: https://stackoverflow.com/questions/778 ... factors-of
Мобильная версия