【问题标题】:Input an Integer and get its prime factors and duplicates [duplicate]输入一个整数并得到它的素数和重复项[重复]
【发布时间】:2012-11-07 17:36:06
【问题描述】:

可能重复:
Prime Factors In C#

我试图让这个编码给我输入的整数的所有素因子,包括它的重复项。我有这个当前的代码,它似乎正在工作,但它并没有显示它的所有主要因素和重复项。任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1_Numeric_Problem
{
    class Program
    {
        static void Main(string[] args)
        {
            string myInput;
            int myInt;
            int p;

            Console.WriteLine(("Please input an integer"));

            myInput = Console.ReadLine();
            myInt = Int32.Parse(myInput);

            {
                for (int i = 2; i > 1; i++)
                {
                    if (i == 100000)

                        break;

                    if (myInt % i == 0)
                    {

                        if (i <= 3)
                        {

                            Console.Write("{0} ", i);
                            Console.ReadLine();
                            continue;
                        }

                        else
                        {
                            for (p = 2; p < i; p++)
                                if (i % p != 0)
                                {
                                    Console.Write("{0} ", i);
                                    Console.ReadLine();
                                    return;
                                    Console.ReadLine();
                                }
                                else
                                {
                                    p++;
                                    continue;
                                }

                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 请编辑您的样本,使其不会将读/写与计算结果混合。并且可能完全删除输入,因为它与问题无关。并添加实际问题...

标签: c# primes


【解决方案1】:

尝试替换以下代码:

for (p = 2; p < i; p++) {
    if (i % p != 0) {
        Console.Write("{0} ", i);
        Console.ReadLine();
        return;
        Console.ReadLine();
    } else {
        p++;
        continue;
    }
}

用这个代替:

bool isPrime = true;

for (p = 2; p <= Math.Sqrt(i); p++) {
    if (i % p == 0) {
        isPrime = false;
        break;
    }

    if (isPrime) {
        Console.Write("{0} ", i);
        Console.ReadLine();
    }
}

【讨论】:

    【解决方案2】:

    你不能像这样做一个for循环吗?

    for (int i = 2; i < myInt; i++)
    {
        if(myInt % i == 0)
            //Do something with it.
    }
    

    【讨论】:

      【解决方案3】:

      使用试除法进行整数分解的基本算法从2开始尝试每个潜在因子,如果它除n,输出因子,减少n,然后搜索下一个因素;请注意,如果 f 除以 n,则它不会增加,因为它可能会再次除以减少的 n。当 f 大于 n 的平方根时,循环停止,因为此时 n 必须是素数。这是伪代码:

      function factors(n)
          f := 2
          while f * f <= n
              if n % f == 0
                  output f
                  n := n / f
              else
                  f := f + 1
          output n
      

      有更好的方法来分解整数,但这应该可以帮助您入门。当你准备好更多时,我在我的博客上谦虚地推荐这个essay

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 2016-07-26
        • 2020-10-27
        • 2016-01-10
        • 2021-02-05
        • 1970-01-01
        相关资源
        最近更新 更多