- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
public class LargestPrimeFactor {
/**
* @param args the command line arguments
*/
public static boolean isPrime(long x){
for (int y=2; y<x; y++){
if (x%y==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("Find out the largest prime factor of 600851475143");
long num=600851475143L;
int largest=0;
int y=0;
for (int x=2; x<num; x++){
if (num%x==0&&isPrime(x)==true){
System.out.println(x);
}
}
System.out.println(largest);
}
这是输出:
Find out the largest prime factor of 600851475143
71
839
1471
6857
-716151937
-408464633
-87625999
-10086647
-5753023
-1234169
-486847
-104441
-59569
-6857
-1471
-839
-71
-1
Exception in thread "main" java.lang.ArithmeticException: / by zero
at largestprimefactor.LargestPrimeFactor.main(LargestPrimeFactor.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 minutes 10 seconds)
如何打印出最大的数字?当“x”应该不断增加而不是减少时,为什么输出显示负数?
这是我编辑的代码:
public static boolean isPrime(long x){
for (int y=2; y<x; y++){
if (x%y==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("Find out the largest prime factor of 600851475143");
long num=600851475143L;
long largest=0;
int y=0;
for (long x=2; x<num/2; x++){
if (num%x==0&&isPrime(x)==true){
System.out.println(x);
if (largest<x){
largest=x;
}
}
}
System.out.println(largest);
}
这是新的输出
Find out the largest prime factor of 600851475143
71
839
1471
6857
0
BUILD SUCCESSFUL (total time: 318 minutes 31 seconds)
如何将最终答案打印为“6857”而不是“0”?另外,如果您注意到的话,该程序的运行时间略多于 5 小时。如何加快此过程?
最佳答案
您正在为 for
循环使用 int
,该循环已溢出。使用 long 变量进行迭代。
for (int x=2; x<num; x++){
^^^
负值的原因是它正在环绕。如果您只想找到最大的,您可以使用一个新的 long
变量来存储迄今为止的 max-seen 。另一种方法是简单地反向迭代范围。
此外,您可以缩短循环以仅检查给定数字的平方根。如果找到一个因子,您可以执行(num/ans)
来找到较大的因子。
关于java - 项目欧拉数 3 : Largest prime factor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540557/
假设我有一个看起来像这样的数据框 A 0 17 1 21 2 18 3 11 4 4 5 27 6 21 7 11 8 7 9 4 10 7
当一周的“最大的一天”不存在时如何获取数据? 例如,如果我的数据库中不存在特定一周的星期五(假设星期六和星期日不在我的数据库中),我仍然希望能够获取星期四的数据。如果星期五和星期四都不存在,我想获取星
#include int main (void) { int phy,geo,i,highest,largest; int arr[2]={phy,geo}; printf(
这个问题是在一次采访中被问到的,我仍在寻找最佳解决方案。 给你一个有 N 个单元格的迷宫。每个单元格可以有多个入口点,但导出点不能超过一个(即入口/导出点是单向门,如阀门)。 单元格以从 0 开始的整
给定一个大的稀疏矩阵(比如 10k+ x 1M+),我需要找到一个子集,它不一定是连续的,由构成密集矩阵(所有非零元素)的行和列组成。我希望这个子矩阵在某些纵横比约束下尽可能大(不是最大和,而是最
题目地址:https://leetcode-cn.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目描述
题目地址:https://leetcode-cn.com/problems/count-largest-group/ 题目描述 Given an integer n. Each number fr
题目地址:https://leetcode.com/problems/largest-sum-of-averages/description/ 题目描述: Wepartition a row of
题目地址:https://leetcode.com/problems/largest-triangle-area/description/ 题目描述 Youhave a list of point
我使用 fastgreedy.community 生成一个社区对象,其中包含 15 个社区。但是我怎样才能在这 15 个社区中提取最大的社区呢? Community sizes 1 2 3
问。给定两个长度相等的数组 A 和 B,找到索引 [i,j] 的最大可能连续子数组,使得 max(A[i: j]) B[j]: Bq.pop() Bq.app
问。给定两个长度相等的数组 A 和 B,找到索引 [i,j] 的最大可能连续子数组,使得 max(A[i: j]) B[j]: Bq.pop() Bq.app
我想使用以下规则从文件夹中检索文件: 获取最大的文件 如果文件大小相同,则采用最新的文件。 到目前为止我已经尝试过以下方法: List folderFilePaths = new ArrayList(
我想知道我的思路是否正确。 我正在准备面试(作为一名大学生),我遇到的其中一个问题是找到数组中 K 个最大的数字。 我的第一个想法是只使用部分选择排序(例如,从第一个元素开始扫描数组,并为看到的最低元
我需要使用动态规划在零和一的矩阵中找到最大的三角形。所以如果这是我的矩阵: 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 1 然后有两个最大
给定一个元素数组,找到最大可能的数字通过使用数组的元素形成。 例如:10 9 答:910 2 3 5 78 答:78532 100 9 答:9100 我知道这个问题有一个使用自定义字符串比较器的解决方
我正在调试内存不足的异常。当我得到异常时,“虚拟字节”性能计数器指示有足够的可寻址空间。然而,问题在于可寻址空间碎片化严重,并且“最大空闲区域”(从 WinDbg 中的 !address 返回)太
题目地址:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目描述 Find the kth
题目地址:https://leetcode.com/problems/largest-time-for-given-digits/description/ 题目描述 Given an array
如何从java中的链表中删除最大元素?我知道我可以使用 get() 或 remove() 函数来检索/删除元素。但我想让它变得高效。我想使用迭代器。你知道我该怎么做吗?请注意,我不想创建自己的链表。我
我是一名优秀的程序员,十分优秀!