recursion

Implements neper number (e) with a recursion function

。_饼干妹妹 提交于 2019-12-23 16:51:05
问题 I want to calculate Neper number( e ) with a recursion function. I have the formula to calculate it: e = (1/0!) + (1/1!) + (1/2!) + (1/3!) +. . . I have the code but it won't work properly: #include <iostream> using namespace std; double f(double res,int i, int n){ return (i == n) ? res: res = res + (1 /f(res,i+1,n)*i); } int main(){ cout << f(1,1,2) << endl; } The result of this code is 2.5 but it should be 2 . Where is the problem? 回答1: Still not sure what you want res for. In fact, if I

Recursively creating a JSON tree, adding only at the deepest level

两盒软妹~` 提交于 2019-12-23 15:47:25
问题 I want to create a JSON hierarchy structure of unknown objects, so it must be handled recursively. Here's my function, where angular.element.isEmptyObject() is inherited from jQuery, and angular.copy() is a function that creates a deep copy of the object (I'm using AngularJS). function recurseTree(tree, newKey, newId) { if(angular.element.isEmptyObject(tree)) { tree[newKey] = {_id: newId}; } else { for(var key in tree) { if(typeof tree[key] == 'object') recurseTree(tree[key], newKey, newId);

F# adding polynomials recursively

一曲冷凌霜 提交于 2019-12-23 15:45:00
问题 I'm trying to write a function in F# that adds polynomials recursively. My polynomials can be represented as a list of tuples. For example, 2x^4 + 3x^2 + x + 5 is equal to [(2.0,4);(3.0,2);(1.0,1);(5.0,0)] All polynomials are properly structured (no repeated terms with the same degree, no terms with zero coefficients unless it is the zero polynomial, terms sorted by decreasing exponent no empty input list). I'm having trouble doing this. Here is my code type term = float * int type poly =

Java inheritance and recursion

倖福魔咒の 提交于 2019-12-23 15:39:03
问题 I have a superclass and a subclass as follows: class Tree{ .. public void add(..){ //makes a call to protected function add(..) }//for client to use. protected TreeNode add(..){}//recursive function which calls itslef } class Stree extends Tree{ //overrides the recursive add function from class Tree protected TreeNode add(..){ .. super.add();//calls the non-recursive add function in superclass. } } The problem here is that when I call super.add() from the new add function in the subclass, it

Recursive function examples in VB.Net

允我心安 提交于 2019-12-23 12:46:05
问题 I have seen other posts, but they are mostly in C#. For someone looking to learn recursion, seeing real world working examples in VB.Net could prove helpful. It's an added difficulty to try to decipher and convert C# if someone is just getting their feet wet programming in VB.Net. I did find this post which I understand now, but if there had been a post of VB.Net examples, I may have been able to pick it up faster.This is in part why I am asking this question: Can anyone could show some

Tail recursion in NodeJS

允我心安 提交于 2019-12-23 12:44:24
问题 So I recently came across case I needed to write code where callback calls itself and so on and wondered about NodeJS and tail-call support, so I found this answer https://stackoverflow.com/a/30369729 saying that yup, it's supported. So I tried it with this simple code: "use strict"; function fac(n){ if(n==1){ console.trace(); return 1; } return n*fac(n-1); } fac(5); Using Node 6.9.2 on Linux x64 and run it as node tailcall.js --harmony --harmony_tailcalls --use-strict and result was: Trace

python recursive variables referenced or copied?

≯℡__Kan透↙ 提交于 2019-12-23 12:24:46
问题 I have the following recursive function, and I'm having trouble figuring out how python handles variables in recursive functions. Will it create a copy of the addresses variable for every recursion, or will it overwrite the variable and create a horrible mess? def get_matches(): addresses = get_addresses() #do stuff for addr in addresses: #do stuff if some_condition: get_matches() else: return 回答1: The underlining concept your looking for is called a frame . Inside of the Python interpreter

Finding the factorial using recursion with the BigInteger Class

好久不见. 提交于 2019-12-23 12:16:43
问题 So consider the following program-segment! I've tried to use the basic recursion function to determine the factorial of a number, but now using the BigInteger class. public static BigInteger fact(int a) { BigInteger factorial = BigInteger.ONE; BigInteger factz = BigInteger.ONE; if(a == 1) { return factorial; } else { return factz.multiply(fact(a-1)); } } So when I try implementing this in a program, it returns the output as 1. Is it because BigInteger objects are immutable? Or am I missing

Recursion vs. Stack

僤鯓⒐⒋嵵緔 提交于 2019-12-23 12:16:31
问题 I wonder if recursion is the only solution to a problem, then is iteration with stacks the only other solution? I think they're kind of equivalent: If recursion works, then iteration will work for sure, and vice versa. Also, I'm not sure why recursion is considered inefficient and often causes stack overflows while iteration with stacks does not. Recursion just uses stacks in an automatic way invisible to the user. 回答1: Although dancancode's answer talks about different kinds of problems like

Stored procedure with recursive call using mysql

二次信任 提交于 2019-12-23 11:48:13
问题 enter image description hereget legside from binary I have tried using stored procedure with recursive call. i need to display relevant name based on emp_name based on leg1 but it showing error like #1414 - OUT or INOUT argument 2 for routine sample.getVolume is not a variable or NEW pseudo-variable in BEFORE trigger Here is my code, DELIMITER $$ CREATE PROCEDURE getVolume( IN param_name VARCHAR(255), OUT result VARCHAR(255 )) BEGIN SELECT val INTO result FROM employee WHERE emp_name = param