Binary search doesn't stop? [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 2 years ago.
Improve this question
I have the following binary search method and the following driver code. Both of the values I search for show in the output are present in the array.
Search Method
//Method for binary search. This method will also cut our array
public static int binarySearch(int[] nums, int x) {
//Bounds
int l = 0, r = nums.length - 1;
//While the size of the array is not 1
while (l <= r) {
//Middle element
int m = l + (r - 1) / 2;
//If our element is the middle
if (nums[m] == x) return m;
//If x is greater, cut to right half
else if (x > nums[m]) l = m + 1;
//Else, ignore right half
else r = m - 1;
}
//If we didn't find the element
return -1;
}
Driver code and output
public class searcher {
public static void main(String[] args) {
/* Initialize a new scanner for user input, initialize random for the
computer to pick a number */
Scanner s = new Scanner(System.in);
//Variable for user input
int guess;
//Do-while loop
do {
System.out.println("Enter a number to search for (0 to quit): ");
//Get the user's guess
guess = s.nextInt();
//Search for the guess in the array of numbers
int i = binarySearch(nums, guess);
System.out.println(i);
//If the number is not found
if (i == -1) {
System.out.println("Your number does not occur in this list.");
}
//If it is
else {
System.out.println("Your number occurs at position " + i);
}
} while (guess != 0);
}
}
/*
Output
Enter a number to search for (0 to quit):
1
1
Your number occurs at position 1
Enter a number to search for (0 to quit):
90
<------- Program doesn't stop running from here...? */
I'm expecting to get an output for the index of the number entered if its found, and if not, the method should return -1 so I can print not found

You are subtracting 1 twice.
r = nums.length - 1;
and then
int m = l + (r - 1) / 2;
should be
int m = l + (r - l) / 2;

int m = l + (r - 1) / 2; // this is not correct, you are subtracting "1"
you need to subtract "left" (variable names edited for clarity):
int mid = left + (right - left) / 2;
or, a bit better:
int mid = (left+ right) >>> 1;

Related

Largest number of times square root can be calculated on numbers between 2 intervals

I wrote a simple program to calculate the maximum number of times square root can be calculated on a number , input is an interval from num1 to num2
eg:
if the input is (1,20), answer is 2, since square root of 16 is 4 , and square root of 4 is 2 .
int max = 0;
for (int i = num1; i <= num2; i++) {
boolean loop = true;
int count = 0;
int current = i;
if (i == 1) {
count++;
} else {
while (loop) {
double squareRoot = Math.sqrt(current);
if (isCurrentNumberPerfectSquare(squareRoot)) {
count++;
current = (int) squareRoot;
} else {
loop = false;
}
}
}
if (count > max) {
max = count;
}
}
return max;
static boolean isCurrentNumberPerfectSquare(double number) {
return ((number - floor(number)) == 0);
}
I get the answer, but was wondering wether this can be improved using some mathematical way ?
Any suggestions ?
To avoid more confusion here my final answer to this topic.
A combination of both previously mentioned approaches.
What 'Parameswar' is looking for is the largest perfect square formed by the lowest base.
Step 1 -
To get that calculate the largest possible perfect square based on your num2 value.
If it is outside your range, you have no perfect square within.
Step 2 -
If it is within your range, you have to check all perfect square formed by a lower base value with a higher number of times.
Step 3 -
If you find one that is within your range, replace your result with the new result and proceed to check lower values. (go back to Step 2)
Step 4 -
Once the value you check is <= 2 you have already found the answer.
Here some sample implementation:
static class Result {
int base;
int times;
}
static boolean isCurrentNumberPerfectSquare(double number) {
return ((number - Math.floor(number)) == 0);
}
private static int perfectSquare(int base, int times) {
int value = base;
for (int i = times; i > 0; i--) {
value = (int) Math.pow(base, 2);
}
return value;
}
private static Result calculatePerfectSquare(int perfectSquare) {
Result result = new Result();
result.base = (int) Math.sqrt(perfectSquare);
result.times = 1;
while (result.base > 2 && isCurrentNumberPerfectSquare(Math.sqrt(result.base))) {
result.base = (int) Math.sqrt(result.base);
result.times += 1;
}
System.out.println(perfectSquare + " -> " + result.base + " ^ " + result.times);
return result;
}
static int maxPerfectSquares(int num1, int num2) {
int largestPerfectSqr = (int) Math.pow(Math.floor(Math.sqrt(num2)), 2);
if (largestPerfectSqr < num1) {
return 0;
}
Result result = calculatePerfectSquare(largestPerfectSqr);
int currentValue = result.base;
while (currentValue > 2) {
// check lower based values
currentValue--;
int newValue = perfectSquare(currentValue, result.times + 1);
if (newValue >= num1 && newValue < num2) {
result = calculatePerfectSquare(newValue);
currentValue = result.base;
}
}
return result.times;
}
Edit - My assumption is incorrect. Refer to the answer provided by "second".
You can remove the outer loop, num2 can be directly used to determine the number with the maximum number of recursive square roots.
requiredNumber = square(floor(sqrt(num2)));
You just need to check to see if the requiredNumber exists in the range [num1, num2] after finding it.
So the refactoring code would look something like this,
int requiredNumber = Math.pow(floor(Math.sqrt(num2)),2);
int numberOfTimes=0;
if(requiredNumber>=num1) {
if (requiredNumber == 1) {
numberOfTimes=1;
} else{
while (isCurrentNumberPerfectSquare(requiredNumber)) {
numberOfTimes++;
}
}
}
Edit 4: for a more optimal approach check my other answer.
I just leave this here if anybody wants to try to follow my thought process ;)
Edit 3:
Using prime numbers is wrong, use lowest non perfect square instead
Example [35,37]
Edit 2:
Now that I think about it there is a even better approach, especially if you assume that num1 and num2 cover a larger range.
Start with the lowest prime number 'non perfect square' and
calculate the maximum perfect square that fits into your range.
If you have found one, you are done.
If not continue with the next prime number 'non perfect square'.
As a example that works well enough for smaller ranges:
I think you can improve the outerloop. There is no need to test every number.
If you know the smallest perfect square, you can just proceed to the next perfect square in the sequence.
For example:
[16, 26]
16 -> 4 -> 2 ==> 2 perfect squares
No neeed to test 17 to 24
25 -> 5 ==> 1 perfect square
and so on ...
#Chrisvin Jem
Your assumption is not correct, see example above
Edit:
Added some code
static int countPerfectSquares(int current) {
int count = 0;
while (true) {
double squareRoot = Math.sqrt(current);
if (isCurrentNumberPerfectSquare(squareRoot)) {
count++;
current = (int) squareRoot;
} else {
return count;
}
}
}
static boolean isCurrentNumberPerfectSquare(double number) {
return ((number - Math.floor(number)) == 0);
}
static int numPerfectSquares(int num1, int num2) {
int max = 0;
if (num1 == 1) {
max = 1;
}
int sqr = Math.max(2, (int) Math.floor(Math.sqrt(num1)));
int current = (int) Math.pow(sqr, 2);
if (current < num1) {
current = (int) Math.pow(++sqr, 2);
}
while (current <= num2) {
max = Math.max(countPerfectSquares(current), max);
current = (int) Math.pow(++sqr, 2);
}
return max;
}

Trying to compare two ints

This is my first time asking a question and I have tried to search the existing threads first. My program is meant to ask the user to enter a 5-digit number and it will check to see if it is a palindrome by reversing the number and then comparing the original number to the reverse. I also put in some verification steps there to reject the number if it is longer or shorter than 5 digits. Everything seems to work until it gets to the part comparing the original number and the reversed number. Here is my code:
import java.util.Scanner;
public class Palindromes {
public static void main(String args[]) {
int n, reverse = 0;
System.out.println("Enter a 5-digit integer to see if it is a palindrome.");
Scanner in = new Scanner(System.in);
n = in.nextInt();
int length = String.valueOf(n).length();
while (length > 5 || length < 5) {
System.out.println("Error: integer must be 5 digits in length.");
System.out.println("Enter a 5-digit integer.");
n = in.nextInt();
length = String.valueOf(n).length();
}
while (length == 5 && n != 0) {
reverse = reverse * 10;
reverse = reverse + n % 10;
n = n / 10;
}
System.out.println("Reversed number is: " + reverse);
if (n == reverse) {
System.out.println("Congratulations! Your number is a palindrome!");
} else {
System.out.println("Sorry. Your number isn't a palindrome.");
}
}
}
Look at what you are doing here!
while (length == 5 && n != 0) {
reverse = reverse * 10;
reverse = reverse + n % 10;
n = n / 10; // <----- You are changing "n"!
}
Which means that after the loop, n will no longer be the same n that the user entered.
To fix this, copy n to another variable and modify that instead.
int temp = n;
while (length == 5 && temp != 0) {
reverse = reverse * 10;
reverse = reverse + temp % 10;
temp = temp / 10;
}
it's more easy to convert the number to an array and reverse it instead of calculating it:
let reverseNumber = parseInt(12345.toString().split("").reverse().join());

Find middle digit in an integer in Java

I have an integer in java "1234567" and my program finds middle digit in a set of integer, is there more optimized way than below code?. Recently asked in java interview.
What I have done is first find no of digits, first, last and middle indexes. Then find middle digit again iterating on same integer. Please advice some optimization.
int a1 = 1234567;
int a = a1;
// calculate length
int noOfDigits = 0;
while(a!=0)
{
a = a/10;
noOfDigits++;
}
int first = 0;
int last = noOfDigits-1;
int middle = (first+last)/2;
boolean midExists = ((a1%2)==1);
System.out.println(" digits: "+a1);
System.out.println(" no of digits "+noOfDigits);
System.out.println(" first "+first);
System.out.println(" last " + last);
if(midExists)
{
System.out.println(" middle " + middle);
int i = last;
int middleDigit = 0;
a = a1;
while(i != middle)
{
a = (a / 10);
middleDigit = (a%10);
i--;
}
System.out.println("middle digit: " + middleDigit);
}
else
System.out.println(" Mid not Exists.. ");
Program Output:
digits: 1234567
no of digits 7
first 0
last 6
middle 3
middle digit: 4
You can also do this in one pass. Idea is that first store the integer in the another variable. Then move two digits to the left in one integer while only one digit in the another one.
int a1 = 1234567;
int a2 = a1;
int flag=0;
while(a2>0)
{
a2/=10; //Moves to the left by one digit
if(a2==0) //If there are odd no. of digits
{
flag=1;
break;
}
a2/=10; //Moves to the left by one digit
a1/=10; //Moves to the left by one digit
}
System.out.print(flag!=1?"No Mid Exists":a1%10);
Your "math" is working correctly. The one thing you can: compute the length (number of digits) within your number upfront, to avoid "iterating" the number twice - so you can determine if that number of digits is even or odd without "iterating" the number:
int n = 1234;
int length = (int)(Math.log10(n)+1);
should give you 4 for 1234, and 5 for 12345.
But beyond that: you can express information in different ways. For example: you can turn an int value into a string.
String asStr = Integer.toString(123456);
And now: you can easily check the length of that string; and you can directly access the corresponding character!
The only thing to keep in mind: characters representing numbers like '1', '2', ... have different numerical values as int 1, 2, ... (see an ASCII table; as '1' is 49 when regarding its numerical value)!
this answer has less code, but wouldn't take much in performance i think:
int a1 = 12334;
int a = a1;
int middle = 0;
int noOfDigits = 0;
while (a1 != 0) {
a1 = a1 / 10;
noOfDigits++;
}
if (noOfDigits % 2 == 1) {
for (int i = 0; i < (noOfDigits / 2) + 1; i++) {
middle = a % 10;
a = a / 10;
}
System.out.println(middle);
} else {
System.out.println("No mid existing");
}
Using only math
int num = 123406789;
int countDigits = (int)Math.ceil(Math.log10(num));
int midIndex = (int)Math.ceil(countDigits/2);
int x = num / (int)Math.pow(10, midIndex);
int middleDigit = x % 10;
System.out.println(middleDigit);

Why isn't my "While Loop" computing and printing out a simple 3N+1 equation?

So I had to write a simple code that would compute the 3N+1 equation; where N is an integer user types in and if it is a positive integer than N = N / 2 and if negative integer than N = N * 3 + 1.
However from what I can understand, my code doesn't work after the first while loop and hence prints nothing. What am I doing wrong? New to programming and still learning so I appreciate your help :)
Code:
import java.util.Scanner;
public class ThreeNplusOneProgram {
public static void main(String[] args) {
int N; Scanner input = new Scanner(System.in); int counter;
System.out.println("Please Enter an integer: ");
N = input.nextInt();
while ( N <= 0 ) {
System.out.println("ERROR: Please Enter an integer greater than zero: ");
N = input.nextInt();
}
//So far we know that N is great than Zero
System.out.println(N);
counter = 1;
while ( N != 1 ) {
if (N == N % 2 )
N = N / 2;
else N = N * 3 + 1;
counter = counter + 1;
}
System.out.println("There were" + counter + "terms in the sequence");
}
}
That is wrong: if (N == N % 2 )
N % 2 returns 1 or 0. You should use if (0 == N % 2 ) to check for being odd / even.
The problem is your if (N == N % 2), you might just want to check if (N >= 0) since you do state that you are looking to check if it is a positive integer.

How does this prime number test in Java work?

The code snippet below checks whether a given number is a prime number. Can someone explain to me why this works? This code was on a study guide given to us for a Java exam.
public static void main(String[] args)
{
int j = 2;
int result = 0;
int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
number = reader.nextInt();
while (j <= number / 2)
{
if (number % j == 0)
{
result = 1;
}
j++;
}
if (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
else
{
System.out.println("Number: " + number + " is Prime. ");
}
}
Overall theory
The condition if (number % j == 0) asks if number is exactly divisible by j
The definition of a prime is
a number divisible by only itself and 1
so if you test all numbers between 2 and number, and none of them are exactly divisible then it is a prime, otherwise it is not.
Of course you don't actually have to go all way to the number, because number cannot be exactly divisible by anything above half number.
Specific sections
While loop
This section runs through values of increasing j, if we pretend that number = 12 then it will run through j = 2,3,4,5,6
int j = 2;
.....
while (j <= number / 2)
{
........
j++;
}
If statement
This section sets result to 1, if at any point number is exactly divisible by j. result is never reset to 0 once it has been set to 1.
......
if (number % j == 0)
{
result = 1;
}
.....
Further improvements
Of course you can improve that even more because you actually need go no higher than sqrt(number) but this snippet has decided not to do that; the reason you need go no higher is because if (for example) 40 is exactly divisible by 4 it is 4*10, you don't need to test for both 4 and 10. And of those pairs one will always be below sqrt(number).
It's also worth noting that they appear to have intended to use result as a boolean, but actually used integers 0 and 1 to represent true and false instead. This is not good practice.
I've tried to comment each line to explain the processes going on, hope it helps!
int j = 2; //variable
int result = 0; //variable
int number = 0; //variable
Scanner reader = new Scanner(System.in); //Scanner object
System.out.println("Please enter a number: "); //Instruction
number = reader.nextInt(); //Get the number entered
while (j <= number / 2) //start loop, during loop j will become each number between 2 and
{ //the entered number divided by 2
if (number % j == 0) //If their is no remainder from your number divided by j...
{
result = 1; //Then result is set to 1 as the number divides equally by another number, hergo
} //it is not a prime number
j++; //Increment j to the next number to test against the number you entered
}
if (result == 1) //check the result from the loop
{
System.out.println("Number: " + number + " is Not Prime."); //If result 1 then a prime
}
else
{
System.out.println("Number: " + number + " is Prime. "); //If result is not 1 it's not a prime
}
It works by iterating over all number between 2 and half of the number entered (since any number greater than the input/2 (but less than the input) would yield a fraction). If the number input divided by j yields a 0 remainder (if (number % j == 0)) then the number input is divisible by a number other than 1 or itself. In this case result is set to 1 and the number is not a prime number.
Java java.math.BigInteger class contains a method isProbablePrime(int certainty) to check the primality of a number.
isProbablePrime(int certainty): A method in BigInteger class to check if a given number is prime.
For certainty = 1, it return true if BigInteger is prime and false if BigInteger is composite.
Miller–Rabin primality algorithm is used to check primality in this method.
import java.math.BigInteger;
public class TestPrime {
public static void main(String[] args) {
int number = 83;
boolean isPrime = testPrime(number);
System.out.println(number + " is prime : " + isPrime);
}
/**
* method to test primality
* #param number
* #return boolean
*/
private static boolean testPrime(int number) {
BigInteger bValue = BigInteger.valueOf(number);
/**
* isProbablePrime method used to check primality.
* */
boolean result = bValue.isProbablePrime(1);
return result;
}
}
Output: 83 is prime : true
For more information, see my blog.
Do try
public class PalindromePrime {
private static int g ,k ,n =0,i,m ;
static String b ="";
private static Scanner scanner = new Scanner( System.in );
public static void main(String [] args) throws IOException {
System.out.print(" Please Inter Data : ");
g = scanner.nextInt();
System.out.print(" Please Inter Data 2 : ");
m = scanner.nextInt();
count(g,m);
}
//
//********************************************************************************
private static int count(int L, int R)
for( i= L ; i<= R ;i++){
int count = 0 ;
for( n = i ; n >=1 ;n -- ){
if(i%n==0){
count = count + 1 ;
}
}
if(count == 2)
{
b = b +i + "" ;
}
}
System.out.print(" Data : ");
System.out.print(" Data : \n " +b );
return R;
}
}

Categories