我的程序不适用于日食..而且我不知道它有什么问题.. Plz帮助.. 并且PLZ也说明原因。
public class ProblemThree {
public static void main(String args[])
{
long a=0L, z=0L;
long n=600851475143L;
for(long i=2;i<=n ;++i)
{
if(600851475143L % i==0)
{
a=i;
if(a%2==0)
{; }
else if(a%3==0)
{ ;}
else if(a%5==0)
{ ;}
else if(a%7==0)
{ ;}
else if (a>z)
{
z=a;
}
}
}
System.out.println(z);
}
}
答案 0 :(得分:2)
谢谢你们的反馈,但我已经用以下代码解决了这个问题.. :)
公共类ProblemThree {
public static void main(String args[])
{
long n=600851475143L;
for(long i=2;i<n ;++i)
{
while(n % i==0)
{//for yes
n=n/i;
}
}
System.out.println(n);
}
}
答案 1 :(得分:1)
你的节目不是&#34;返回&#34;什么,因为它仍然在运行。你的循环仍然是迭代的。您的代码需要修改才能更高效。这是我对同一问题的解决方案。
long testNum = 600851475143l;
int largestFactor = 0;
long loopMax = 17425170l; //largest known prime
for (int i = 3; i * i <= loopMax; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime && testNum % i == 0) {
System.out.println("prime factor: " + i);
largestFactor = i;
loopMax = (testNum / i) + 1;
}
}
System.out.println("result is: " + largestFactor);
答案 2 :(得分:0)
package Euler;
import java.util.Scanner;
public class euler3 {
public static void main(String args[])
{
long num;
Scanner sc = new Scanner(System.in);
num= sc.nextLong();
for(int i=2;i<num; i++)
{
while(num%i == 0)
{
//System.out.println(i);
num=num/i;
}
}
if(num>2)
System.out.println(num);
}
}
13195的主要因子是5、7、13和29。
600851475143的最大素数是6857。
答案 3 :(得分:-1)
#include <bits/stdc++.h>
using namespace std;
int SieveOfEratosthenes(int x,long long signed int n)
{
bool prime[x]; //use sieve upto square root of
// required number
memset(prime, true, sizeof(prime));
for (long long signed int p = 2; p * p <= x; p++)
{
if (prime[p] == true)
{
for (long long signed int i = p * p; i <= x; i += p)
prime[i] = false;
}
}
int res;
for (long long signed int p = 2; p <= x; p++)
if (prime[p])
{
if(n%p==0) //if number is divisble by prime number than update with
res=p; //latest value of prime number
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout<<SieveOfEratosthenes(775146,600851475143);//Return the answer
// long long signed int n;cin>>n; for value of n
//cout<<SieveOfEratosthenes(sqrt(n),n);
return 0;
}