C#中的主要因素

时间:2011-05-03 16:58:08

标签: c# primes prime-factoring

我想在C#2005中创建一个程序来计算给定输入的素数因子。我想使用基本和最简单的东西,不需要为它创建方法也不需要为数组等事物创建简单的模数。是否有任何代码能满足我的需求?

这里是查找简单因子的代码,我需要修改此代码来计算素因子

class Program
{
    static void Main(string[] args)
    {
        int a, b;
        Console.WriteLine("Please enter your integer: ");
        a = int.Parse(Console.ReadLine());
        for (b = 1; b <= a; b++)
        {
            if (a % b == 0)
            {
                Console.WriteLine(b + " is a factor of " + a);
            }
        }
        Console.ReadLine();



    }
}

6 个答案:

答案 0 :(得分:38)

int a, b;
Console.WriteLine("Please enter your integer: ");
a = int.Parse(Console.ReadLine());
for (b = 2; a > 1; b++)
    if (a % b == 0)
    {
        int x = 0;
        while (a % b == 0)
        {
            a /= b;
            x++;
        }
        Console.WriteLine("{0} is a prime factor {1} times!", b, x);
    }
Console.WriteLine("Th-Th-Th-Th-Th-... That's all, folks!");

Works on my machine!

答案 1 :(得分:2)

public static List<int> Generate(int number){
    var primes = new List<int>();

    for(int div = 2; div<=number; div++){
        while(number%div==0){
            primes.Add(div);
            number = number / div;
        }
    }
    return primes;
}

如果您想学习开发步骤,可以watch video here

答案 2 :(得分:1)

你可以更好一点,因为除数永远不会大于数字的平方根。

     for(int div = 2; div<=Math.Sqrt(number); div++)

答案 3 :(得分:0)

此版本将所有因素列为显式公式:

static void Main(string[] args)
    {
        Console.WriteLine("Please enter your integer (0 to stop): ");

        int a = int.Parse(Console.ReadLine());
        while(a>0)
        {
            List<int> primeFactors = PrimeFactors(a);
            LogFactorList(primeFactors);
            a = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("Goodbye.");
    }


    /// <summary>
    /// Find prime factors
    /// </summary>
    public static List<int> PrimeFactors(int a)
    {
        List<int> retval = new List<int>();
        for (int b = 2; a > 1; b++)
        {               
            while (a % b == 0)
            {
                a /= b;
                retval.Add(b);
            }               
        }
        return retval;
    }

    /// <summary>
    /// Output factor list to console
    /// </summary>
    private static void LogFactorList(List<int> factors)
    {
        if (factors.Count == 1)
        {
            Console.WriteLine("{0} is Prime", factors[0]);
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < factors.Count; ++i)
            {
                if (i > 0)
                {
                    sb.Append('*');
                }
                sb.AppendFormat("{0}", factors[i]);
            }
            Console.WriteLine(sb.ToString());
        }
    }

答案 4 :(得分:0)

尝试使用此代码(我在此代码中合并了各种解决方案)。虽然,插值不是在2005年(我想是....)

因此,无论如何,请尝试以下操作:

// C# Program to print all prime factors 
using System; 

namespace prime
{
    public class Prime
    { 

        public static void PrimeFactors(int n)
        {
            Console.Write($"Prime Factors of {n} are:  ");

            // Prints all the numbers of 2  
            while (n % 2 == 0)
            {
                Console.Write("2 ");
                n /= 2;
            }

            // As no 2 can be further divided, this probably means that n
            // is now an odd number
            for(int i = 3; i <= Math.Sqrt(n); i += 2)
            {
                while (n % i == 0)
                {
                    Console.Write($"{i} ");
                    n /= i;
                }
            }

            // This is for case if n is greater than 2
            if (n > 2)
            {
                Console.Write($"{n} ");
            }

        }

        // Prompts user to give Input to number and passes it on 
        // the PrimeFactors function after checking its format
        public static void RunPrimeFactors()
        {
            Console.Write("Enter a number: ");
            if (int.TryParse(Console.ReadLine(), out int n))
            {
                PrimeFactors(n);
            }
            else
            {
                Console.WriteLine("You entered the wrong format");
            }

        }
        // Driver Code 
        public static void Main()
        {
            RunPrimeFactors();
        }

    }
}

答案 5 :(得分:0)

using static System.Console;

namespace CodeX
{
    public class Program
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 20; i++)
            {
                if (IsPrime(i))
                {
                    Write($"{i} ");
                }
            }
        }

        private static bool IsPrime(int number)
        {
            if (number <= 1) return false;  // prime numbers are greater than 1

            for (int i = 2; i < number; i++)
            {
                // only if is not a product of two natural numbers
                if (number % i == 0)       
                    return false;
            }

            return true;
        }
    }
}