私に述べられた問題はこれでした:
「数 600851475143 の最大の素因数は?」
このプログラムは、答えを見つけるために使用されます。C を使用すると、まさにこれでした。
#include<math.h> // for remainder because % does not work with double or floats
#include<stdio.h>
main()
{
double x=600851475143,y=3.0;
while(x!=y) // divide until only the number can divide itself
{
if(remainder((x/y),1)==0.0) // if x is divisible by y which means it is a factor then do the magic
{
x=x/y; // divide the number x by y thereby reducing one constituent factor
}
y=y+2; // add 2 simply because only odd numbers can be prime and hence only prime numbers can be prime factors
}
printf("%lf",y); // do the printing magic
}
質問は、x をチェックしてすべての奇数で除算しようとすることですが、すべての奇数が素数ではないことに注意してください。アルゴリズムのこの欠陥により、実際には私は素因数のチェック (奇数因数ではない)。
驚くべきことに、このプログラムが吐き出す答えは正しいので、答えを確認しました。
どうすればこれを回避できますか?それは意味がありません。