时间执行期间暗战 [英] Time Running out during execution
本文介绍了时间执行期间暗战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是从项目欧拉问题3。什么是600851475143.我做节目最大的主要因素,但这个数量问题是一个12digit号码,我不得不采取的数据类型,只要长。当我尝试执行对小数目,如13195的程序,它工作正常,但所需数量不是!我试着在各种编译器运行它在线,并在Cygwin,我在我的电脑上使用。但是,这个项目停止三个输出后,向前迈进。
HELP!
#包括LT&;&stdio.h中GT;
诠释的main()
{
长长的NUM = 600851475143;
INT I = 1,J,K,大= 0;
//的printf(\\ n输入一个数字:);
// scanf函数(%D,试验#);
而(I< = NUM)
{
K = 0;
如果(NUM%我== 0)
{
J = 1;
而(J< = I)
{
如果(我引用%j == 0)
ķ++;
J ++;
} 如果(K == 2)
{
的printf(\\ n%d是一个首要因素,我);
如果(I>大)
大= I;
}
}
我++;
} 的printf(\\ nBiggest主要因素为%d,大);
返回0;
}
解决方案
#包括LT&;&stdio.h中GT;诠释主(){
长长的NUM = 600851475143LL;
长长的I = 1; //它必须是时的主要是同一类型的NUM。 对于(i = 2; I * I< = NUM ++我){
而(NUM%我== 0){
NUM / = I;
}
}
的printf(\\ nBiggest首要因素是LLD%,NUM == 1 I - 1:NUM);
返回0;
}
This is 3rd Problem from Project Euler. What is the biggest prime factor of 600851475143. I've done the program but this number in question being a 12digit number, I had to take the datatype as long long. When I try to execute the program for small number, such as 13195, it works fine but for the desired number it doesn't! I tried running it in various compilers online and also on Cygwin which I use on my PC. But the programs stops going forward after three outputs.
HELP!!
#include<stdio.h>
int main()
{
long long num = 600851475143;
int i=1,j,k, big=0;
// printf("\nEnter a number:");
//scanf("%d",&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j = 1;
while( j <= i)
{
if( i % j == 0)
k++;
j++;
}
if(k==2)
{
printf("\n%d is a prime factor",i);
if( i > big)
big = i;
}
}
i++;
}
printf("\nBiggest prime factor is %d", big);
return 0;
}
解决方案
#include<stdio.h>
int main(){
long long num = 600851475143LL;
long long i=1;//It must be the same type for num when was the prime.
for(i=2;i*i<=num;++i){
while(num % i ==0){
num /= i;
}
}
printf("\nBiggest prime factor is %lld", num == 1 ? i - 1 : num);
return 0;
}
这篇关于时间执行期间暗战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文