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;
}
}
Related
Can someone please help to tell why I'm getting zero in my arithmetic operations?
I'm trying to solve this arithmetic problem which gives value of x and y but it's giving me java.lang.arithmeticException error or showing zero result.
It will be really helpful to me.
this is my input
a=6,b=10,c=8,d=12,e=800,f=900
**This linear equation to obtain the values for x and y can be solved using
x = (ed -fb)/(ad -bc), y = (fa -ec)/(ad – bc)**
this is the problem that i'm trying to solve.
public class linearequation {
public static void main(String[] args){
Scanner scn= new Scanner(System.in);
linear lin1 = new linear(scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt());
if(lin1.isSolvable()) {
System.out.println(lin1.getx());
System.out.println(lin1.gety());
}else {
System.out.println("No Solution");
}
}
}
class linear {
private int a, b, c, d, e, f;
int x, y;
int den = ((a * d) - (b * c));
public linear(int na, int nb, int nc, int nd, int ne, int nf) {
na = a;
nb = b;
nc = c;
nd = d;
ne = e;
nf = f;
}
public int geta() {
return a;
}
public int getb() {
return b;
}
public int getc() {
return c;
}
public int getd() {
return d;
}
public int gete() {
return e;
}
public int getf() {
return f;
}
public int getx() {
return x = ((e * d) - (f * b)) / den;
}
public int gety() {
return y = ((f * a) - (e * c)) / den;
}
public boolean isSolvable() {
if (den <= 0) {
return false;
} else {
return true;
}
}
}```
As far as I can see your constructor for linear is the problem.
You're passing na, nb, nc, nd, ne, nf into it and reassign them to be the value of a, b, c, d, e, and f, respectively, when you actually want to assign the other way around, e.g. a = na instead of na = a.
Also, you're setting den before the values for a...f are set by way of the constructor. den is never reassigned and thus stays 0.
This is what your constructor should look like:
public linear(int na, int nb, int nc, int nd, int ne, int nf) {
a = na;
b = nb;
c = nc;
d = nd;
e = ne;
f = nf;
den = ((a * d) - (b * c));
}
1.The method takes two arguments for calculating the greatest common divisor.
2.Instead of returning the values of a or b, the program returns -1.
public static int gcd(int a,int b)
{
if(a==0 && b>0)
{
// returns when a becomes 0
return b;
}
else if(b==0 && a>0)
{
//returns when b becomes 0
return a;
}
else if(a>b)
gcd(b,a%b);
else
gcd(a,b%a);
return -1;
}
You need to return the recursive calls too. So no need to return -1
public static int gcd(int a, int b) {
if (a == 0 && b > 0) {
// returns when a becomes 0
return b;
} else if (b == 0 && a > 0) {
//returns when b becomes 0
return a;
} else if (a > b) {
return gcd(b, a % b);
} else {
return gcd(a, b % a);
}
}
So i have a little problem with reducing a negative fraction
This is my reduce code
private void reduce() {
int g = Helper.gcd(this.num, this.den);
num /= g;
den /= g;
}
For example:
8/64 gives 1/8
But giving -8/64 let's the program crash
This is my gcd code
public static int gcd(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
You need to extract the sign first.
private void reduce() {
boolean neg = (num < 0) != (den < 0);
num = Math.abs(num);
den = Math.abs(den);
// obtain the GCD of the non-negative values.
int g = Helper.gcd(num, den);
num /= g;
den /= g;
if (neg) num *= -1;
}
Your gcd method only works for positive numbers. Negative numbers and zero need to be handled separately.
public static int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) {
if (b == 0)
throw new IllegalArgumentException();
return b;
}
if (b == 0)
return a;
// The rest is just your code, unchanged.
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
Hello I'm trying to make a code that takes three integers from the command line and sorts them into the min, mid, and max values. I can't figure out the mid programming. It won't always sort them properly. Can you help?
public class SortInteger{
public static int max3(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
public static int min3(int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;}
public static int sort(int a, int b, int c){
int sort = a;
if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;
if (sort > c && sort < a) sort = c;
else sort =b;
if (sort > c && sort < b) sort = c;
else sort = b;
if (sort > a && sort < b) sort = c;
else sort = c;
return sort;
}
public static void main(String[] args){
int a= Integer.parseInt(args [0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
StdOut.println("Min is " + min3(a, b, c));
StdOut.println("Mid is " + sort(a, b, c));
StdOut.println("Max is " + max3(a, b, c));
}
}
Try:
public static int mid(int a, int b, int c){
return a + b + c - max(a,b,c) - min(a,b,c);
}
Also for the min and max just use Math:
public static int min(int a, int b, int c){
return Math.min(Math.min(a,b),c);//Replace with Math.max for max.
}
You're stepping all over your own toes inside the sort function. Take, for instance, your first two if statements:
if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;
If a is between b and c, your first if statement will be true, and sort will be kept as the value of a. But, then consider your next one. The value in a will not be greater than a, so the second if statement will be false, and change sort to c, even though you already found a to be the middle value. Not what you wanted. To fix this, you could change the code you execute when your if statements are true to just return the value of sort. So, like:
if (sort > b && sort < c) return sort;
else sort = b;
if (sort > a && sort < c) return sort;
else sort =c;
// etc.
Try the following:
public class SortInteger
{
//use general sorting algorithm for arbitrary length, this is for 3 length specifically
public static int[] sort(int[] inputs)
{
int k;
if(inputs[0] >= inputs[1])
{
k = inputs[0];
inputs[0] = inputs[1];
inputs[1] = k;
}
if(inputs[1] >= inputs[2])
{
k = inputs[1];
inputs[1] = inputs[2];
inputs[2] = k;
}
//incase our last element is less than our first we repeat:
if(inputs[0] >= inputs[1])
{
k = inputs[0];
inputs[0] = inputs[1];
inputs[1] = k;
}
return inputs
}
public static void main(String[] args)
{
int[] x = new int[3];
x[0] = Integer.parseInt(args[0]);
x[1] = Integer.parseInt(args[1]);
x[2] = Integer.parseInt(args[2]);
x = sort(x);
System.out.println("min is: " + x[0]);
System.out.println("mid is: " + x[1]);
System.out.println("max is: " + x[2]);
}
}
public class Solution {
public int pow(int A,int B,int d)
{
if(A<0){ A=A+d;}
if (B==0)
{
if(A==0){return 0;}
return 1;
}
else if(B%2==0)
{
int y=pow(A,B/2,d);
return (y*y)%d;
}
else
{
return (A%d*pow(A,B-1,d))%d;
}
}
}
My code overflows for,
A : 71045970
B : 41535484
d : 64735492
my code gives o/p: -17412928
expected o/p : 20805472
Where it goes wrong?
can someone modify my code?
Please try this
public int Mod(int a, int b, int c) {
if(b==0){
if(a==0) return 0;
else
return 1;
}
else if(b%2==0){
long y=Mod(a,b/2,c);
return (int)(((long)(y*y))%(long)c);
}else{
int k=a%c;
if(k<0){
k+=c;
}
return (int)(((long)((long)k * (long)Mod(a,b-1,c)))%(long)c);
}
}
BigInteger as a modPow method that does this for you trivially.
Not giving your expected result but giving a different result:
public int pow(int a, int b, int mod) {
if (a < 0) {
a = a + mod;
}
if (b == 0) {
if (a == 0) {
return 0;
}
return 1;
} else if (b % 2 == 0) {
int y = pow(a, b / 2, mod);
return (y * y) % mod;
} else {
return (a % mod * pow(a, b - 1, mod)) % mod;
}
}
public int bigPow(int a, int b, int mod) {
return BigInteger.valueOf(a).modPow(BigInteger.valueOf(a), BigInteger.valueOf(mod)).intValue();
}
private void test(int a, int b, int mod) {
System.out.println("Old - modPow(" + a + "," + b + "," + mod + ") = " + pow(a, b, mod));
System.out.println("New - modPow(" + a + "," + b + "," + mod + ") = " + bigPow(a, b, mod));
}
public void test() {
test(71045970, 41535484, 64735492);
}
prints
Old - modPow(71045970,41535484,64735492) = -17412928
New - modPow(71045970,41535484,64735492) = 44382800
In case you actually aren't looking for modPow (which now looks likely) here's a rough attemt at duplicating youyr aalgorithm using BigInteger.
public BigInteger bigPow(BigInteger a, BigInteger b, BigInteger mod) {
if (a.compareTo(BigInteger.ZERO) < 0) {
a = a.add(mod);
}
if (b.compareTo(BigInteger.ZERO) == 0) {
if (a.compareTo(BigInteger.ZERO) == 0) {
return BigInteger.ZERO;
}
return BigInteger.ONE;
} else if (!b.testBit(0)) {
BigInteger y = bigPow(a, b.shiftRight(1), mod);
return y.multiply(y).mod(mod);
} else {
return a.mod(mod).multiply(bigPow(a, b.subtract(BigInteger.ONE), mod));
}
}
Now gives the expected answer.
Old - modPow(71045970,41535484,64735492) = -17412928
New - modPow(71045970,41535484,64735492) = 20805472