Easier way to switch variables? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a simple method that goes like this:
public int gcd(int a, int b) {
while (a!=b) {
int q = b;
b = a%b;
a = q;
}
return a;
}
Is there an easier way to write a greatest common denominator? Particularly the three lines in the while loop, can they be simplified?

If you don't mind performance but just readability then choose a different approach:
public int gcd(int a, int b) {
BigInteger ba = BigInteger.valueOf(a);
BigInteger bb = BigInteger.valueOf(b);
return ba.gcd(bb).intValue();
}

Try this:
public static int gcd(int a, int b) {
int q = b;
b = a % b;
a = q;
return (a != b && a != 0 && b != 0) ? gcd(a, b) : a - b;
}

Related

Where did I messed up? Why is my method returning Infinity? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
public class Vehicle {
int passengers;
int fuelcap;
int mph;
Vehicle(int p, int f, int m) {
p = passengers;
f = fuelcap;
m = mph;
}
double fuelNeeded (int miles) {
return (double) miles/mph;
}
}
public class STUFF {
public static void main(String[] args) {
Vehicle minivan = new Vehicle(7, 16, 21);
Vehicle sportscar = new Vehicle(2, 14, 12);
double gallons;
int dist = 252;
gallons = minivan.fuelNeeded(dist);
System.out.println(gallons);
gallons = sportscar.fuelNeeded(dist);
System.out.println(gallons);
}
}
Output:
Infinity
Infinity
I have been stuck on this problem for quite sometimes now, I'm not sure where I messed up but the method keeps outputing the result as Infinity, it would be much helpful if you guys can give me some insights to where and how the code was wrong. Thank you so much!!
You are assigning the local variables (p, f, m) the values of your global variables, but you should be doing it the other way around:
Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mph = m;
}
Because of this, you are dividing by 0 in fuelNeeded, which results in Infinity.

Finding the sum of the two greatest numbers of 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With Java, how would one find the greatest two numbers of a set of 3 numbers without using if conditionals.
For example given the 3 numbers {2,3,5}
int a = 2;
int b = 3;
int c = 5;
int total;
total would be replaced with the value of c+b = 8
List<Integer> data = Arrays.asList(23,6,13);
Collections.sort(data);
Collections.reverse(data)
data = data.sublist(0,2);
System.out.println(data);
One line:
int biggest = (a > b ? a : b) > c ? (a > b ? a : b) : c;
Two lines:
int firstStep = (a > b ? a : b);
int biggest = firstStep > c ? firstStep : c;
Java 8:
int max = Arrays.stream(numbers).max().getAsInt();
int sec_max = Arrays.stream(numbers).filter(i -> i != max).max().getAsInt();

Determine whether a is a factor of b [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Given two integers, a and b, check to see whether a is a factor of b. If a is an integer of b, return true. If not, return false.
Inputs:
a and b - two integers, where a is a potential factor and b is an integer larger than or equal to a; a is going to always be less than or equal to b
Output:
boolean - true if a is a factor of b, false if not
Here's what I tried:
boolean factor( int a, int b ) {
int divider = a%b;
if (divider == 0)
return true;
else
return false;
}
You have your mod statement backwards if you want a to be the smaller number.
int divider = b%a;
To know if b can be divided by a (with 0 remainder):
boolean factor( int a, int b ) {
return b % a == 0;
}
public boolean isFactor(int potential, int number) {
return number%potential== 0;
}
This should solve your problem. You had the order in your modulus wrong.
Start with tests, then implement them. You want to test the 0 case.
#Test
public void test(){
assertFalse(factor(5,6));
assertTrue(factor(4,2));
assertFalse(factor(8,0));
assertTrue(factor(0,9));
}
public boolean factor(int a, int b){
if(b == 0){
return false;
}
return a % b == 0;
}

Java program to compute the value of (2^n); the program should run with Big-O(2^n) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is this java program runs with Big-O(2^n)? if not, any suggestions on how to modify it?
This program calculates the value of 2^n:
public static int exponent(int n) {
if (n == 0)
return 1;
else
return 2 * exponent(n - 1);
}
Yes, it is even in O(n) and therefore also in O(2^n).
To have a big-O which is proportional to the solution it should reduce to 1 i.e. it should make as many calls as the answer.
For there to be O(2^n) time complexity
public static int power(int base, int n) {
return n == 0 ? 1 : multiply(base, power(base, n -1);
}
public static int multiply(int a, int b) {
return a > 0 ? add(multiply(a - 1, b), b) : 0;
}
public static int add(int a, int b) {
return a > b ? 1 + add(a -1, b) : b > 0 ? 1 + add(0, b -1) : 0;
}
To calculate 2^n it will reduce down to 1 + 2^n times.
Good news: it runs in O(n).
Each iteration, n is decremented by 1, so it loops exactly n times
This algorithm has O(n) time complexity. If you add a second parameter, you can make a method with O(log n) time complexity.
public static int power(int base, int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(base * base, n/2);
else
return base * power(base * base, n/2);
}
In your code, n is reduced by 1 every time, so it takes n steps to terminate. With this approach, n is halved every time, so it can finish much more quickly.

More efficient solution to blackjack? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on the following problem from codingbat:
Given 2 int values greater than 0, return whichever value is nearest
to 21 without going over. Return 0 if they both go over.
blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19
My solutions is:
public int blackjack(int a, int b) {
if (a>21){
if (b>21){
return 0;
}
return b;
}
else if(b>21) return a;
return Math.max(a,b);
}
Is there something in my logic that can be improved to make it more efficient? Am I doing something unnecessary?
This could be more efficient. At the very least it's another way of looking at the problem:
public int blackjack(int a, int b) {
if (a>21) a = 0;
if (b>21) b = 0;
if (a>b) {
return a;
else {
return b;
}
}
I wouldn't say this is more efficient, but I re-ordered some of the if statements and arrived at the code below. I think this is, at the very least, somewhat easier to follow:
public int blackjack(int a, int b) {
if (a <= 21 && b <= 21) return Math.max(a, b);
if (a <= 21) return a;
if (b <= 21) return b;
return 0;
}
Using ternary operator:
public int blackjack(int a, int b) {
a = a > 21 ? 0 : a;
b = b > 21 ? 0 : b;
return (a > b) ? a : b;
}
This might be pretty close;
public int blackjack(int a, int b) {
if(a > 21 && b > 21) return 0;
else if (a <= 21 && a > b || b > 21) return a;
return b;
}

Categories