我有这样的事情:
int f = 120;
for(int ff = 1; ff <= f; ff++){
while (f % ff != 0){
}
我的循环查找因素有什么问题吗?我对 for 和 while 语句的工作原理感到非常困惑,所以它们很可能是完全错误的。
在此之后,我将如何将变量分配给所述因素?
我有这样的事情:
int f = 120;
for(int ff = 1; ff <= f; ff++){
while (f % ff != 0){
}
我的循环查找因素有什么问题吗?我对 for 和 while 语句的工作原理感到非常困惑,所以它们很可能是完全错误的。
在此之后,我将如何将变量分配给所述因素?
以下代码将返回给定数字的所有因子的列表:
public ArrayList<Integer> findFactors(int num) {
ArrayList<Integer> factors = new ArrayList<Integer>();
// Skip two if the number is odd
int incrementer = num % 2 == 0 ? 1 : 2;
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
// If there is no remainder, then the number is a factor.
if (num % i == 0) {
factors.add(i);
// Skip duplicates
if (i != num / i) {
factors.add(num / i);
}
}
}
// Sort the list of factors
Collections.sort(factors);
return factors;
}
这个答案以两种方式改进了Sharad Dargan 的答案:
根据此答案中使用的想法,您可以通过根据数字是偶数还是奇数确定要增加的值来加快解决方案。
在 for 循环之前添加以下代码行:
int incrementer = num % 2 == 0 ? 1 : 2;
然后将循环的最后一部分更改为:
i += incrementer
如果数字是奇数,它将跳过所有偶数,而不是无论如何总是加一。
Sharad 将上限值存储在一个变量中,然后在 for 循环中使用该变量:
int upperlimit = (int)(Math.sqrt(a));
...
for(int i = 1; i <= upperlimit; i+= 1)
相反,Math.sqrt(num)
直接放在 for 循环中并跳过上限变量:
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
这将允许您跳过代码的强制转换部分,创建更清晰的代码。
然后可以使用一些 JUnit 测试用例:
@Test
public void test12() {
FindFactors find = new FindFactors();
int num = 12;
List<Integer> factors = Arrays.asList(1, 2, 3, 4, 6, 12);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test1000000() {
FindFactors find = new FindFactors();
int num = 1000000;
List<Integer> factors = Arrays.asList(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200,
250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500,
15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test1() {
FindFactors find = new FindFactors();
int num = 1;
List<Integer> factors = Arrays.asList(1);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test0() {
FindFactors find = new FindFactors();
int num = 0;
List<Integer> factors = new ArrayList<Integer>();
assertEquals(factors, find.findFactors(num));
}
这是获取给定数字的所有因子的方法。
public class Factors {
public static void main(String[] args){
int n = 420;
for(int i=2; i<=n; i++){
while(n%i==0){
System.out.println(i + "| " + n);
System.out.println(" -----");
n = n/i;
}
}
}
}
输出:
2| 420
-----
2| 210
-----
3| 105
-----
5| 35
-----
7| 7
-----
public class Solution {
public ArrayList<Integer> allFactors(int a) {
int upperlimit = (int)(Math.sqrt(a));
ArrayList<Integer> factors = new ArrayList<Integer>();
for(int i=1;i <= upperlimit; i+= 1){
if(a%i == 0){
factors.add(i);
if(i != a/i){
factors.add(a/i);
}
}
}
Collections.sort(factors);
return factors;
}
}
上面的解决方案就像计算素数一样简单。不同之处在于我们不断计算产品的其他部分,即 reqd 数。
为了找到给定数字的因数,您只需要检查给定数字的平方根即可。
例如,为了找到 6 的因数,您只需要检查到 2.45 (√6)。6 的因数是 1 和 2,以及它们的倒数,即 3 和 6。
我制作了一个程序来确定给定数字的因子并显示它们。这是必要的代码:
Scanner input = new Scanner(System.in);
System.out.print("Enter integer: ");
long num = input.nextLong();
for(long i = 1; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
System.out.println(i);
if(i != num/i) {
System.out.println(num/i);
}
}
}
你只需要这个程序来找到给定数字的因数。但是,如果您想更进一步并按升序显示因子,则必要的代码如下:
Scanner input = new Scanner(System.in);
System.out.print("Enter integer: ");
long num = input.nextLong();
ArrayList<Long> list1 = new ArrayList<>(), list2 = new ArrayList<>();
long currentTime = System.currentTimeMillis();
for(long i = 1; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
list1.add(i);
if(i != num/i) {
list2.add(num/i);
}
}
}
int n1 = list1.size() - 1;
int n2 = list2.size() - 1;
for(int i = 0; i <= n1; i++) {
System.out.println(list1.get(i));
}
for(int i = n2; i >= 0; i--) {
System.out.println(list2.get(i));
}
这是做什么的:这个程序将数字的因数存储在一个列表(list1)中,并将这些数字的倒数存储在另一个列表(list2)中。然后它打印两个列表的元素(如图所示)。
您的循环没有任何问题for
,但是在while
这里使用循环是错误的。for
你的循环逻辑是:
ff
为 1。ff <= f
。for
循环中的所有操作后,将 1 添加到ff
.这看起来就像你想要的那样。
但是,while
循环是不对的。只要ff
是 的一个因素,它就会继续执行您在那里编写的任何代码f
,因此除非您在while
代码中更改它们,否则您将获得无限循环。但是,将其更改为if
声明将为您提供所需的内容。
由于您正在检查因子,因此您实际上不需要检查直到 f 的所有可能性 - 只需要检查 f 的平方根。每当您发现这ff
是一个因素时,都输出ff
和f/ff
作为因素,除非f
是一个平方数。
public static void printFactors(int number) {
if (number < 1 )
System.out.println("Invalid Value");
for (int i = 1 ; i <= number ; ++i) {
if ( number % i == 0)
System.out.println(i);
}
}
}
看起来您不会在 while 循环中f
或ff
在您的 while 循环中做某事?如果是这样,则表达式f%ff != 0
要么为假(然后将转到 for 循环中的下一个),要么为真,并以无限循环结束。
你确定你需要这样的时间吗?
稍微修改的解决方案:您可以先检查变量 x 是否可被变量 y 整除。如果是,我们将计数 1 并重复此过程。对于循环计数器,使用 x/y 并且您应该检查 x>0 以避免当 x 变为零但循环尚未完成时重复。
public class Factor {
public static void main(String[] args) {
int x = 48;
int x1 = x;
int y = 2;
int k = x / y;
int j = 0;
for (int i = 1; i < k; i++) {
if ((x % y) == 0 && x > 0)
j++;
x = x / 2;
}
System.out.println(+x1 + " is a factor of " + y + " for " + j
+ " times.");
}
}
import java.util.Scanner;
public class Factors
{
Scanner scn=new Scanner(System.in);
int num=scn.nextInt();
public void findFactor()
{
System.out.println("Factors are");
System.out.println("1");
for(int i=2;i<=num;i++)
{
if(num%i==0)
{
num=num/i;
System.out.println(i);
i=2;
}
}
}
public static void main(String[] args)
{
while(1==1)
{
System.out.println("Enter a Number");
Factors fct=new Factors();
fct.findFactor();
}
}
}
利用 Java 8 中引入的 Streams,以下将打印给定数字的因子。
int input = 1500;
IntStream.rangeClosed(1, input)
.filter(e -> input % e == 0)
.forEach(System.out::println);
这就是你像老板一样自己写的方式。需要添加 if 语句来处理一和二,但除此之外;这种方法很性感
public static void primerize(int n){
boolean reduced = false;
while(n > 2){
if(n%2 == 0){
System.out.println(2 + "," + n/2);
n /= 2;
}
else{
int i = isPrime(n);
if(i == n && reduced == false){
System.out.println(1 + "," + n);
n /= n;
}
else if(i == n){
n/= n;
}
else{
System.out.println(i + "," + n/i);
n = i;
reduced = true;
}
}
}}
public static int isPrime(int n){
for(int i = (n/3); i > 0; i--){
if(i == 1){
return n;
}
else if(n%i == 0){
return i;
}
}
return 0;}
我得到了所有因素都很好(我刚刚修改了问题中的算法)。
int num1 = 120;
for(int num2=1;num2<=num1;num2++)
{
if (num1%num2 != 0)
System.out.println(num2);
}
此代码将为您提供因素。
ArrayList<Integer> arr = new ArrayList<>();
int x=48;
int y=1;
while(x!=1)
{
if(x%y==0)
{
x=x/y;
arr.add(y);
if(y==1)
{
y++;
}
}
else
{
y+=1;
}
}
System.out.println(arr);
使用递归函数的最简单方法
public static int factorial(int n){
if(n!=1)
return n*factorial(n-1);
return 1;
}