[英]Creating recursive static method to find product of prime numbers in array
The number 9 keeps returning true from my Prime method despite if(number % j == 0) returning true when 9/3 with no remainders but 0. Would someone like to help me with this?尽管 if(number % j == 0) 在 9/3 时返回 true 并且没有余数但为 0,但数字 9 一直从我的 Prime 方法返回 true。有人愿意帮助我吗? I'm also trying to not use for loops.
我也试图不使用 for 循环。 It's for a homework assignment.
这是为了家庭作业。
Edit: I've attempted the answer below but I still get the same issue using an if statement.编辑:我已经尝试了下面的答案,但我仍然使用 if 语句遇到同样的问题。
public class test {
private static int product = 1;
private static int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
private static boolean allowmultiply;
public static void main(String args[]) {
System.out.println("Product: " + findproduct(array, 0));
}
public static int findproduct(int[] list, int i) {
int[] temp = list;
if(i <= temp.length-1) {
allowmultiply = Prime(temp[i], 2);
if(allowmultiply != false) {
product = product * temp[i];
}
i++;
return findproduct(temp, i);
}
return product;
}
public static boolean Prime(int number, int j) {
if(j <= number/j) {
if(number % j == 0) {
return false;
}
Prime(number, j+1);
}
return true;
}
}
NEW CODE: Thanks to WJS for the help!新代码:感谢 WJS 的帮助!
public class ProductPrimeNumber {
private static int product = 1;
private static boolean allowmultiply;
private static int checker = 0;
private static int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public static void main(String args[]) {
System.out.println("Product: " + findproduct(array, 0));
}
public static int findproduct(int[] list, int i) {
int[] array = list;
if(i <= array.length-1) {
allowmultiply = isPrime(array[i], 2);
if(allowmultiply != false) {
product = product * array[i];
}
checker = 0;
i++;
return findproduct(array , i);
}
return product;
}
public static boolean isPrime(int number, int i) {
if(i < number) {
if(number % i == 0 && i != number) {
checker++;
}
isPrime(number, i+1);
}
if(checker > 0) {
return false;
}
return true;
}
}
This may help.这可能会有所帮助。 Forget about primes for the moment as that is an entirely separate issue.
暂时忘掉素数,因为那是一个完全独立的问题。 Consider the product of even numbers.
考虑偶数的乘积。 The method is not as compact as it could be to aid clarity.
该方法并不像帮助清晰度那样紧凑。
static Predicate<Integer> isEven = a -> a != 0 && a % 2 == 0;
int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int prod = findProduct(data, 0);
System.out.println("prod = " + prod);
prints印刷
prod = 3840
i == v.length-1
(the last element).i == v.length-1
(最后一个元素)。"unwind"
the recursive calls by first testing the even parity of v[i]
v[i]
的偶校验来"unwind"
递归调用prod * v[i]
.prod * v[i]
。prod
.prod
。public static int findProduct(int[] v, int i) {
int prod = 1;
if (i < v.length-1) {
prod = findProduct(v, i+1);
}
if (isEven.test(v[i])) {
return prod * v[i];
}
return prod;
}
The only difference between this and the prime solution is what your testing for.这个和主要解决方案之间的唯一区别是您的测试目的。 So replace
isEven
with isPrime
.所以用
isEven
替换isPrime
。 Then you just need to focus on your isPrime
code.然后你只需要专注于你的
isPrime
代码。
The more compact version of findProduct
is here.更紧凑的
findProduct
版本在这里。
i == i.length
, return 1
to start the unwinding processi == i.length
时,返回1
开始展开过程public static int prodEven(int[] v, int i) {
if (i < v.length) {
return (isEven.test(v[i]) ? v[i] : 1) * prodEven(v, i+1);
}
return 1;
}
To do via recursion, you can start with a index ( say i that points to the starting of the array ).要通过递归完成,您可以从一个索引开始(比如 i 指向数组的开头)。
The base condition: when i reaches the length of the array, then return 1;基本条件:当i达到数组长度时,则返回1;
work to be done: in case the number is prime, multiply it with product and the return value from the calls of further functions and in case it is not, multiply the product with the return values from the calls of further functions.要做的工作:如果数字是素数,则将其乘以乘积和调用其他函数的返回值,如果不是,则将乘积乘以调用其他函数的返回值。
public class PrimeProduct {
static int product = 1;
public static void main(String[] args) {
int list[] = { 3, 5, 9, 11 }; // 11*5*3
int res = findProduct(list, 0);
System.out.println(res);
}
public static int findProduct(int[] list, int i) {
if (i == list.length) {
return 1;
}
return isPrime(list[i], 2) ? product * list[i] * findProduct(list, i + 1) : product * findProduct(list, i + 1);
}
private static boolean isPrime(int num, int i) {
if (i * i > num) {
return true;
}
return (num % i != 0) ? isPrime(num, i + 1) : false;
}
}
and the output is output 是
165
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.