Write a method
boolean perfectSquare(int n)
that returns true if n is a perfect square. You may not use
Math.sqrt and the runtime must be O(logn).
Using this hint: Binary search to try to find a number whose square is n. Initialize low = 0 and high = n. Note that mid*mid may overflow, so you can avoid this by using a long.
boolean perfectSquare(int n)
{
long low=0;
long high=n; //Intialzing the
variables hogh and low
int
d=binarySearch(low,high,n); //Calling BinarySearch function
if(d==1) //checking the
returning value
return true;
else
return false;
}
int binarySearch(long low,long high,int n) //Function implementing
the binary Search
{
if(high>=low)
{
long mid=(high+low)/2;
if(mid*mid==n)
return 1;
else if(mid*mid>n) return
binarySearch(low,mid-1,n);
else return
binarySearch(mid+1,high,n);
}
else return -1;
}
//Tester program for running a sample testcase:
import java.util.*;
import java.lang.*;
import java.io.*;
public class Abc
{
boolean perfectSquare(int n)
{
long low=0;
long high=n; //Intialzing the
variables hogh and low
int
d=binarySearch(low,high,n); //Calling BinarySearch function
if(d==1) //checking the
returning value
return true;
else
return false;
}
int binarySearch(long low,long high,int n) //Function implementing
the binary Search
{
if(high>=low)
{
long mid=(high+low)/2;
if(mid*mid==n)
return 1;
else if(mid*mid>n) return
binarySearch(low,mid-1,n);
else return
binarySearch(mid+1,high,n);
}
else return -1;
}
public static void main (String[] args) throws
java.lang.Exception
{
Abc chg=new Abc();
boolean
isPerfect=chg.perfectSquare(288);
System.out.println(isPerfect);
}
}
Write a method boolean perfectSquare(int n) that returns true if n is a perfect square. You...
Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted array a with n distinct integers, and an integer m. If there are two elements in the array that add to be m, the method returns True; if not, it returns False. You may assume that the elements in the array are all distinct. There are several ways to solve this problem! (and some solutions are worth more points than others!) For 6 points,...
please write this in "MARIE assembly language" #include <iostream> using namespace std; int DivideByTwo(int, int); // Data section int Data[] = { 0x0102, 0x0105, 0x0106, 0x0108, 0x011A, 0x0120, 0x0225, 0x0230, 0x0231, 0x0238, 0x0339, 0x0350, 0x0459, 0x055F, 0x066A, 0x0790, 0x08AB, 0x09AF, 0x0AB9, 0x0BBD, 0x0CC1, 0x0DCA, 0x0EFE, 0x0FFE }; int main() { int* BAddr = &Data[0]; int* EAddr = &Data[23]; int Count = 24; // the number of Data int Ffff = 0xffff; // value for "not found" int num; // input...
Qi. Create a java project Question that includes: • A bubble sort method to sort your arrays. Implement the below recursive binary search. • A java main class where you: o Create an array of characters that contains the letters of your first name followed by your last name, without spaces. o Call the sorting method to sort the array o Call binary search method to find the position of the first letter 'a' in your array. // Recursive Binary...
java programming.
One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000
So. I'm sick and I need help figuring out whatever is going on with this. #include using namespace std; //prototypes int getExchangesInBubbleSort(int[], int); int getExchangesInSelectionSort(int[], int); int main() { char choice; cout << "1. Search Benchmarks\n"; cout << "2. Sorting Benchmarks\n"; do { int input; cout << "Please enter the program you would like to run. \n"; cin >> input; if (input == 1)...
Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n using //recursion. Note: 2^3 = 2 * 2^2 . 2^2 = 2 * 2^1 2^1 = 2 What value/values of n will stop the recursion (base case)? Sample output: 5 2 to 5 is 32
We need to write a method that, given a key as an argument, returns the next in order key found in the binary search tree. If the key given as an argument is not found, the method should still return the next in order key. If the binary tree is empty or all the stored keys are smaller than the argument, then the return value should be empty. For example, using a collection of {10,13,52,67,68,83} stored in the binary search...
Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...