public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
GCD(a1, b1);
}
return 1;
}
}
Why does this implementation of Euclid's Algo (in Java) always return 1.? How do I get it to return the right answer?
You forgot the return statement. You made the recursive call, but did not use the value.
public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
// return here
return GCD(a1, b1);
}
return 1;
}
Although, you can write it succinctly like this:
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Or in one line:
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
I think you simply missed a return, so you code just runs to the end and returns with 1.
public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
return GCD(a1, b1);
}
return 1;
}
}
I'm having a problem finding the "NChooseR" value when the user enters two numbers, and the program must use recursion. The "NChooseR" formula must be n! / r!(n-r)!
Scanner input = new Scanner(System.in);
System.out.println("This program will calculate the number of ways to chose r different objects from a set of n objects\n");
System.out.println("How many objects would you like to chose? (r value)");
int userR = input.nextInt();
System.out.println("How many objects are there to chose from? (n value)");
int userN = input.nextInt();
System.out.println("There are " + nchooser(userN, userR) + " ways to chose " + userR + " objects from a set of " + userN + " objects");
}
public static long factorialn(int n) {
//return a value of one for terms one and two
if ((n == 1) || (n == 2)) {
return 1;
} else {
return factorialn(n - 1) + factorialn(n - 2);
}
}
public static long factorialr(int r) {
//return a value of one for terms one and two
if ((r == 1) || (r == 2)) {
return 1;
} else {
return factorialr(r - 1) + factorialr(r - 2);
}
}
public static long factorialnr(int r, int n) {
//return a value of one for terms one and two
if ((r == 1) || (r == 2) || (n == 1) || (n == 2)) {
return 1;
} else {
return factorialr((n-r) - 1) + factorialr((n - r) - 2);
}
}
public static long nchooser(int r, int n) {
return factorialn(n) / (factorialr(r) * (factorialnr(n,r)));
}
here is some code that may work with larger numbers:
public static final long f(final int n) { // factorial
long i,p;
if(n<=1)
p=1;
else for(p=n,i=2;i<=n-1;i++)
p=p*i;
return (p);
}
public static final long c(final int n,final int r) { // binomial coefficient
long i,p;
if(r<0||n<0||r>n)
p=0;
else if(r==0)
p=1;
else if(r>n-r)
p=c(n,n-r);
else {
for(p=1,i=n;i>=n-r+1;i--)
p=p*i;
p=p/f(r);
}
return p;
}
I'm just beginner in programming.
uf is a union-find class with the method union which connects the roots of two node.
This piece of code is responsible for opening a site of a grid and union the site with its neighbor if any of neighbor is opened. And if one of its neighbors is full, then fill all nodes that connected with the site.
This is the actual code:
if(i == 1){
uf.union(len*len, xyTo1D(i,j));
if(existAndOpen(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j == len && i == len)){
if(existAndOpen(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
else{
if(existAndFull(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndFull(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndFull(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndFull(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
if(existAndOpen(i+1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndOpen(i,j+1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
}
How can I simplify the code?
Try this
boolean f1(int a, int b) { }
boolean f2(int a, int b) { }
void A(int a, int b) { }
void testAndA(BiPredicate<Integer, Integer> p, int a, int b) {
if (p.test(a, b))
A(a, b);
}
and
if(x == 1){
testAndA(this::f1, x + 1, y);
testAndA(this::f1, x, y + 1);
} else {
testAndA(this::f2, x + 1, y);
testAndA(this::f2, x, y + 1);
}
You can write a loop to loop through all of the different values that could be passed to f1(), such as something like:
for (int deltax = -1; deltax <= 1; deltax++) {
for (int deltay = -1; deltay <= 1; deltay++) {
if (f1(x + deltax, y + deltay)) {
A(x + deltax, y + deltay);
}
}
}
Of course change the start and end values of deltax and deltay depending on which conditions you need to check.
You say there are "more if statements in each block." Reading between the lines, I assume that means you need to make more calls to f1/f2 and A, but with different x and y offsets.
Here is a program that shows a way to refactor the code to avoid coding repetition. Its main features are:
It uses object orientation to abstract the inner if blocks.
It uses an offsets array to represent the x/y offsets for each inner if block.
The doIt() method uses a loop to invoke the inner if blocks.
public class Main {
static interface F {
void f(int i, int j);
}
static class F1Caller implements F {
public void f(int a, int b) {
if (f1(a, b)) {
A(a, b);
}
}
}
static class F2Caller implements F {
public void f(int a, int b) {
if (f2(a, b)) {
A(a, b);
}
}
}
static boolean f1(int a, int b) { System.out.print(" f1. "); return true; }
static boolean f2(int a, int b) { System.out.print(" f2. "); return true; }
static void A(int a, int b) { System.out.println("a: " + a + ", b: " + b); }
static F1Caller f1Caller = new F1Caller();
static F2Caller f2Caller = new F2Caller();
// x and y offsets for each call to f1/f2.
// Add more offset rows, as needed.
static int offsets[][] = {
{1, 0},
{0, -1}
};
static void doIt(int x, int y) {
System.out.println("x: " + x + ", y: " + y);
F f = (x == 1) ? f1Caller : f2Caller;
for (int k = 0; k < offsets.length; k++) {
f.f(x + offsets[k][0], y + offsets[k][1]);
}
}
public static void main(String[] args) {
doIt(0, 0);
doIt(1, 0);
}
}
The output of the above program is:
x: 0, y: 0
f2. a: 1, b: 0
f2. a: 0, b: -1
x: 1, y: 0
f1. a: 2, b: 0
f1. a: 1, b: -1
You can combine the function together.
Using a switch.
public static boolean func(int a, int b, int fun)
{
boolean output = false;
switch(fun)
{
case 1:
//do stuff
output = true;
break;
case 2:
//do stuff
output = true;
break;
default:
//unknown function handling
output = false;
}
return output;
}
Integrate it with a for-loop:
public static void main(String[] args)
{
int i;
//Change this to whatever you want or set it to a argument.
int repeat = 2;
for(i = 1; i <= repeat; i++)
{
func(a, b, i);
}
}
input 3,5 output should be 3,4,5
input 5,3 output should be 5,4,3
And the code
public static void test(int a, int b) {
if(a>b) {
for (int i = a; i >= b; i--) {
System.out.print(i + "\t");
}
}else if(a<b) {
for (int i = a; i <= b; i++) {
System.out.print(i + "\t");
}
}
}
It works but looks a little messy. Is it possible to do without if else thing? Only one loop.
One solution which handle also boundary values correctly could be
public static void test(int start, int end) {
int current = start;
int stepWidth = current <= end ? +1 : -1;
while (current != (end + stepWidth)) {
System.out.print(current + "\t");
current += stepWidth;
}
System.out.println("");
}
edit Another one using a for loop.
public static void test(int start, int end) {
int stepWidth = start <= end ? 1 : -1;
for (int current = start; current != end + stepWidth; current += stepWidth) {
System.out.print(current + "\t");
}
System.out.println("");
}
executions
test(3, 5);
test(5, 3);
test(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
test(Integer.MIN_VALUE, Integer.MIN_VALUE + 3);
output
3 4 5
5 4 3
2147483644 2147483645 2147483646 2147483647
-2147483648 -2147483647 -2147483646 -2147483645
How about this version?
public static void test(int a, int b) {
int d = b > a ? 1 : -1;
for (int i = a; i != b; i+=d) {
System.out.print(i + "\t");
}
System.out.println(b);
}
This my solution, feedback appreciated.
public static void test(int a, int b) {
int middle = (a < b) ? (b - 1) : (a - 1);
System.out.println(a + "," + middle + ","+b);
}
Above will work only when a != b.
I believe my code is now foolproof. I will write up the pseudocode now. But I do have one question. Why does DRJava ask that I return something outside of my if statements? As you can see I wrote for ex: "return 1;" just because it asked. It will never return that value however. Can someone explain this to me?
public class assignment1question2test {
public static void main(String[] args) {
int[] a = new int[50];
int l = 0;
int r = a.length;
for(int i=0; i<r; i++) {
a[i] = 1;
}
a[0] = 10;
for (int i=0; i<r; i++) {
System.out.println(a[i]);
}
System.out.print(recursiveSearch(a,l,r));
}
public static int recursiveSearch (int[] a, int l, int r) {
int third1 = (r-l)/3 + l;
int third2 = third1*2 - l + 1;
System.out.println("i will be checking compare from " + l + " to " + third1 + " and " + (third1 + 1) + " to " + third2);
int compareResult = compare(a,l,third1,third1 + 1, third2);
if(r-l == 1) {
if (compareResult == 1) {
return l;
}
else {
return r;
}
}
if (compareResult == 0) {
return recursiveSearch(a,third2 + 1, r);
}
if (compareResult == 1) {
return recursiveSearch(a,l,third1);
}
if (compareResult == -1) {
return recursiveSearch(a,third1 + 1, third2);
}
return 1;
}
public static int compare(int[] a, int i, int j, int k, int l) {
int count1 = 0;
int count2 = 0;
for(int g=i; g<=j; g++) {
count1 = count1 + a[g];
}
for(int g=k; g<=l; g++) {
count2 = count2 + a[g];
}
if (count1 == count2) {
return 0;
}
if (count1 > count2) {
return 1;
}
if (count1 < count2) {
return -1;
}
return 0;
}
}
UPDATED FINAL PSEUDOCODE:
Algorithm: recursiveSearch (a,l,r)
Inputs: An array a, indices l and r which delimit the part of interest.
Output: The index that has the lead coin.
int third1 ← (r - l + 1)/3
int third2 ← third1*2 - l + 1
if (r-l = 0) then
return l
int compareResult ← compare(a,l,third1,third1 + 1,third2)
if (r-l = 1) then
if (compareResult = 1) then
return l
else
return r
if (compareResult = 0) then
return recursiveSearch(a, third2 + 1, r)
if (compareResult = "1") then
return recursiveSearch(a,l,third1)
if (compareResult = "-1") then
return recursiveSearch(a,third1 + 1,third2)
You seem to be including mid in the following search regardless of which side is larger. The recursive calls should both exclude mid from their search space.
Also, for the comparison to be meaningful, the two groups being compared need to be of equal size. That will require some extra odd/even logic.