Problems with prime numbers

前端 未结 6 1923
礼貌的吻别
礼貌的吻别 2020-12-14 05:08

I am trying to write a program to find the largest prime factor of a very large number, and have tried several methods with varying success. All of the ones I have found so

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 05:30

    Certainly it will work (see Mark Byers' answer), but for "very large" inputs it may take far too long. You should note that your call to getLowestDivisiblePrimeNumber() conceals another loop, so this runs at O(N^2), and that depending on what you mean by "very large" it may have to work on BigNums which will be slow.

    You could speed it up a little, by noting that your algorithm need never check factors smaller than the last one found.

提交回复
热议问题