我的代码出于某种原因似乎忽略了循环

时间:2020-09-30 21:37:36

标签: c# for-loop

我正在尝试解决项目欧拉的第三个问题,但似乎编译器跳过了for循环,因此使我的代码完全没用

注意:这个想法没有显示任何语法错误

这是代码:

class Program
    {
        static void Main(string[] args)
        {
            const long n = 600851475143;
            List<long> factors = new List<long>();
            factors = getFactors(Math.Sqrt(n));
            long max = 0;
            for (int i = 0; i<factors.Count ;i++)//this loop in particular , it doesn't print the "testing.."
            {
                Console.WriteLine("test....");
                if(isPrime(getFactors(factors[i])))
                {
                    max = factors[i];
                }
            }
            Console.ReadKey();
        }
        static List<long> getFactors(double number)
        {
            List<long> list = new List<long>();
            for(int i = 2;i<=number;i++)
            {
                if(number%i ==0)
                {
                    list.Add(i);
                }
            }
            return list;
        }
        static bool isPrime(List<long> list)
        {
            if(list.Count == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

static List<long> getFactors(double number)
{
    List<long> list = new List<long>();
    for (int i = 2; i <= number; i++)
    {
       
        if (Math.Floor(number % i) == 0)
        {
            list.Add(i);
        }
    }
    return list;
}

number是一个分数,除非将其强制转换为int,否则它将永远== 0