recursion

Recursive method 10x slower than interative [closed]

放肆的年华 提交于 2019-12-23 19:08:58
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . The code is cleaned as far as i could to show my problem. Basically its an octree search+intersect. the intersect function comes from an SDK (the whole

Merging two sorted lists into one in descending order

和自甴很熟 提交于 2019-12-23 18:53:39
问题 I've been able to figure out how to merge two sorted lists in ascending order, but I'm having trouble finding a way to do so in the opposite direction. How can I merge the two lists into one in descending order without reversing the string after? 回答1: You just have to do everything in the opposite way--retrieve items from the tail instead of the head, go for the larger of the two when making comparisons, and return the remaining list in reverse order when the other is exhausted: def merge

Can't understand why is prolog looping infinitly

只谈情不闲聊 提交于 2019-12-23 18:53:28
问题 From Bratko's book, Prolog Programming for Artificial Intelligence (4th Edition) We have the following code which doesn't work - anc4(X,Z):- anc4(X,Y), parent(Y,Z). anc4(X,Z):- parent(X,Z). In the book, on page 55, figure 2.15, is shown that parent(Y,Z) is kept calling until stack is out of memory. What I don't understand is that prolog does a recursiv call to anc4(X,Y), and not to parent (Y,Z) first. Why doesn't prolog goes over and over to the first line , anc4(X,Y) , and rather goes to the

Given elements in a array find combinations that equal destination value

狂风中的少年 提交于 2019-12-23 18:51:39
问题 I have been pondering over this problem quite some time and for some reason it is not getting through my head. If i am given an array [1,2,3,4] and a destination value of say 5. I want to get all possible combinations from the array such that they add to the destination value. So one approach i can think of is 1!=5 1+2!=5 1+3!=5 1+4=5 //now check with combos of 3 digits. 1+2+3 !=5 1+2+4 !=5 //now check with combos of 4...however at this point i realized i missed the combination 1+3+4 also. I

scala parser combinator stackoverflow recursion

随声附和 提交于 2019-12-23 18:26:22
问题 The following code example crashes due to stack overflow when parsing an expression deeply nested in brackets. Parser combinators are part of the standard library. Is there a way of making use of the library avoiding that? (I'm not asking for the reason of why it crashes rather for the right way to deal with the standard library.) parsing: ((((((((... 1 + 1 ...))))))))) code: import scala.util.parsing.combinator.syntactical.StandardTokenParsers object ArithmeticParser1 extends

Why does this tail-call recursive fibonacci function break with gcc -O2? [closed]

血红的双手。 提交于 2019-12-23 18:12:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I implemented the following tail-call recursive fibonacci function to try out gcc's Tail Call Optimization (as mentioned here): #include <stdint.h> uint64_t fib_tc( uint8_t n) { uint64_t fib_helper(uint8_t n, uint64_t acc, uint64_t prev) { if(n == 0) return acc; else return fib_helper( n-1, acc+prev, acc); } fib

Java- Flatten an array using recursion

微笑、不失礼 提交于 2019-12-23 18:07:42
问题 I have been practicing algorithms, and recursion is always my weak point. This problem asks to flatten a nested array into a single array. This would be simple if using a loop giving an O(n^3) [given an equally sized 3d array] solution. However with recursion I have been struggling for a few hours. This is what I have, please note I have dabbled with my code trying out different solutions, this is just what I decided to leave it at to post up to you guys. What I want are two things, is there

Reverse array function with one parameter. (JavaScript)

杀马特。学长 韩版系。学妹 提交于 2019-12-23 18:03:58
问题 I'm having a problem trying to write a body for a function that recursively reverse an array, but only has one parameter. function ReverseArray(arr) { var i = 0; var j = arr.length - 1; if (i < j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return ReverseArray(arr); } return arr; } I realize this won't work because the variables will be re-initialized when the function calls itself. I'm just looking for some ideas at this point, because i'm stuck. 回答1: First, for other people who

Python3 Recursivley Sum Digits of an Integer

北城余情 提交于 2019-12-23 17:18:42
问题 I need help implementing a function that will take the digits of an integer and sum them together. As long as the sumDigits function implements recursion, it is valid, and the main function must be left as is. I will include a template below: def sumdigits(value): #recursively sum digits def main(): number=int(input(“Enter a number : ”)) print(sumdigits(number)) main() Thank you 回答1: A very short version: def sumdigits(value): return value and (value % 10 + sumdigits(value // 10)) The value

Prolog convert mins to hours

谁说我不能喝 提交于 2019-12-23 16:56:43
问题 This is the code I have created. mins_to_hours(In, H, M):- In < 60, H = 0, M is In. mins_to_hours(In, H, M):- In >= 60, H is H1+1, In1 is In-60, mins_to_hours(In1, H1, M). It works fine when the minutes are less than 60, e.g. ?- mins_to_hours(20,H,M). H = 0, M = 20 ; false. However when trying to run it for more than 60 minutes ?- mins_to_hours(80,H,M). it outputs an exception ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR: [9] _3198 is _3204+1 ERROR: [8] mins_to_hours(80