Recursion Comments
Comments
-
Why r.binarySearch?
-
Dear admin,
When clicking on the Raw I/O option under Input I see "8":
Input
8 <<<<No Square
25
81
225
841
1024
7056
36
144Correct Output
5
9
15
29
32
84
6
12Your Output (I still have not solved it, so currently empty)
But on the instructions it is mentioned "(To make it simpler, the input will just contain square numbers.)" Is this a typo on the Raw I/O or is it actually handling that? Because on one of my tests i got Stack Overflow so currently not sure if expected...
Thanks in advance,
David.
-
@David, the first number in raw input states how many cases there will be, it's not one of the actual cases.
-
Awesome site this is!
-
how do i know what is desired number? Should I make anarray from 0 to a, than start at midle checking if that number to power 2is a?
-
@armands, you don't actually need an array, but that's the right idea (see the hint).
-
Hi guys, i have written this code https://ideone.com/eFWUiZ its kind of bad, i know it , i suck at recursion but it was a bit logical for me i really don't know where i'm missing up
-
See if you can keep it organized. If you don't have the right number, see if it's larger or smaller and call your recursive method accordingly. You don't want to have two methods calling each other since that's just confusing. Look over the provided code for binary search and see how to adjust it for the "square root search" problem.
(PS - You can link to your learneroo code submission, no need to link elsewhere.)
-
WOOOHOOO! Man I'm pleased with my implementation of binary search.
However, you see how I got the range for iMid and iMax?
I made a crude estimation there... that the square root of any number could certainly be within the range = a/2
But thats not very efficient. How do I choose a smaller range and thus use less cycles on finding the square root?
-
After solving the question here, you can click on the "Featured Answers" link (next to the hint button) to see a possible solution. The idea is to use the same technique as binary search, but in search of the square root (i.e. the number that multiplied by itself equals the original number).
-
Working answer. Refer after trying it out
public static int computeRoot(int mid,int a){ if(mid==1){ return 0; } if(mid*mid == a ){
return mid; }else{ return computeRoot(mid-1,a); } } public static int doStuff(int a){ int mid = a/2; return computeRoot(mid,a); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
-
Getting a stack overflow error but am not sure what is wrong.
//your code here int c=0; if(a==0){ System.out.println(a + " -> "+b); } else{ doStuff(a,b); System.out.println(a + " -> "+b); doStuff(c,b); }
-
I don't know if this is how you thought for us to do it but this works for me.
//your code here
int notUsed = 0;
for (int i = 1; i <= 3; i++){
if(i != a && i != b){
notUsed = i;
}
}
System.out.println(a + "->" + notUsed + " " + a + "->" + b + " " + notUsed + "->" + b); -
@Kevin: you never break the loop.
Hint: never reduces a, so it will never reach 0 to break the loop -
Hi my script is correct (as i was able to test the correct output using another browser)
however im not clear on whats the submition rule for learneroo. Using javascript, does learneroo accept
console.log(Start + "->" + End)?here is my full recursion
function doStuff(a, b){
//your code here
// rule will be, 1st. move to the free peg.
// 2nd . move to the goal peg
// 3rd, move to the goal peg.
fp = 6-a-b;
Calc(2,a,fp,b);
}
//
Calc = function(n,Start,Mid,End){
if(n===0){
console.log (Start+"->" +End);
//document.write(Start+"->"+End);} else{ Calc(n-1,Start,End,Mid); //Calc(1,Start,Mid,End); Calc(n-1,Mid,Start,End); }
};
-
@Ed, just use
print()
to print in Javascript. I'm going to add more help info soon. -
Although I know the process of solution to the problem, I can't figure out how to transform into program. Could anyone give me some hint of how to program it?
-
You can view the beginning of a solution here. The comments there can help guide you to a full solution.
-
Thanks, I have finished the challenge with your hint.
-
public static void doStuff(int a){ solve(1, 3, a); System.out.println(); } static void solve(int a, int b, int elements){
if (elements == 1){ System.out.print(a + "->" + b + " "); } else { int intermediate = intermediate(a, b); solve(a, intermediate, elements - 1); solve(a,b,1); solve(intermediate, b, elements - 1); } } public static int intermediate(int from, int to){ return 6 - (from + to); }
-
Any answers or help for this one? I find the backtracking code suggestion pretty vague... Thanks!
-
See if the new hint help.
-
Thanks! Much clearer.
Greg -
static boolean groupSum(int index, int[] nums, int sum) {
//base case: check if reached sum
if(index==nums.length-1){
if(sum==0)return true;else return false; } if(sum==0) return true; // otherwise check both options: // a - count current number in sum: // b - skip current number: // check if either option reaches sum: return groupSum(index+1,nums,sum-nums[index]) || groupSum(index+1,nums,sum); }
my code doesn't work out :(
anyone can help?
still bit confuse with recursion
Cynthia
Oct 3, 12:51 PMi have tried to compile this on my dev C++ and it works ..