Determine whether a is a factor of b [closed] - java

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;
}

Related

Count Even Digits in an Integer in Java [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 5 years ago.
Improve this question
I am trying to write a method in Java that will return true if an integer only consists of even digits.
For example when the input integer is 2468, the method will return true.
Another example, if the input integer is -68, the method will return true. If the integer consisted of 24638, the method should return false.
I also am trying to use only integers. I do not want to use ToString() to take the length of the integer as my condition in my loop and I also do not want to use an array.
I have searched the forums but most replies seem to use ToString() or arrays of which Iā€™m avoiding.
My code is below:
int countEven = 0;
int countOdd = 0;
if (n == 0)
countEven++;
while (n != 0) {
int lastdigit = n % 10;
if (lastdigit == 0 || lastdigit % 2 == 0)
countEven++;
else
countOdd++;
n = n /10;
}
if (countEven >= 0); return true;
You are not checking the condition for countodd. Lets say number is 235. Your counteven will be 1 and countodd will be 2. Then your last if condition will return true no matter what. Instead try this,
while(n != 0){
int lastdigit = n % 10;
if(lastdigit % 2 == 1)
return false;
n = n /10;
}
return true;
The problem is that your last if statement is not correct.
Try this:
if (countOdd > 0)
return false;
else
return true;

My Java code is not running [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 5 years ago.
Improve this question
If I am not mistaken, this bit of code should print out all even numbers smaller than or equal to 100. When I run this code, nothing happens. No error message or anything. I'm using Eclipse.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder = number % 2;
while(number <= 100) {
number++;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
As others stated before, value of remainder is set only once. However, you check it a hundred times expecting it to tell you something else every time. I'd suggest putting it inside a loop, so you get a "fresh" value for every number.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder;
while(number <= 100) {
number++;
remainder = number % 2;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
The reminder doesn't change as the assignment is prior to while loop. The statement int remainder = number % 2; has to be inside while loop to see the expected result.
for(int i = 0;i<=100; i= i+2)
System.out.println(i);
You do not need remainder :)

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.

Easier way to switch variables? [closed]

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;
}

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