I dont understand recursive functions [closed] - recursion

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Write a recursive function that computes the value of the recursively defined function
F(n)= -F(n-2), F(0)=1 and F(1) = -2. Iv been staring at this for hours i don't understand. Thanks for help.

Like any programming problem, you can first express the solution using pseudo code, and then move on to implementing it in your language of choice, e.g.
FUNCTION F(n)
IF n == 0 -- recursion terminates when n = 0, with result 0
RETURN 0
ELSE IF n == 1 -- recursion can also terminate when n = 1, with result -2
RETURN -2
ELSE -- otherwise recursion contiunes with F(n - 2), F(n - 4), ...
RETURN -F(n - 2) -- until one of the terminating conditions is reached
END
From here it should be quite straightforward to implement this function in C++ or whatever language you choose. Be sure to also implement a "test harness", i.e. a function which calls F with a range of different input values and then prints the result, so that you can verify whether the function is behaving correctly and debug it if necessary.

Related

manipulating lists in common lisp [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how does one create a list from a list,what function can i really use i was thinking of using mapcar or maplist with cons together but im not getting any fruitful results,lets say i have a list (a b) then i want a function that will create a list containing the same elements but they should be inform of lists like this ((a) (b)) ,any ideas on how i can solve this problem?? is there a function a use to it?
if i have a list(a b)
the result should be ((a)(b))
thanks guys
What you want to do is this:
(defun listify(ls)
(mapcar (lambda (elem) (list elem)) ls))
EDIT
Which is the same as (Thanks to #RainerJoswig):
(defun listify(ls)
(mapcar #'list ls))
And if you do:
(listify (list 1 2 3))
or
(listify '(1 2 3))
The output will be:
((1) (2) (3))

Understanding the recursion here [closed]

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 10 years ago.
I'm looking at a recursive problem where a child can hop a staircase of steps n in 1,2 or 3 steps at one time.
The code I'm looking at is a function similar to the likes of fibonacci. However, what I don't get is if n==0 why does it return 1. If the total number of steps are 0, shouldn't there be zero ways of climbing it ? Why is there one way to climb it ?
int f(int n)
{
if(n<0)
return 0;
else if(n==0)
return 1;
else
return f(n-1) + f(n-2) + f(n-3);
}
This is more of a logic question. Suppose you stand there and do nothing. How many steps did you climb? The answer is zero. So, did you successfully climb zero steps? Yes.
How many ways are there to climb zero stairs? Just one way: you have to stand there and not climb any steps.
That isn't really a valid question.
Because this is a recursive implementation, you'll always have to provide a boundary case for f(nmin) where nmin is 1 less than the lowest valid n.
So the case n = 0 is a boundary condition that serves to ensure the correct result for all values where n > 0.
In other words, it (probably) doesn't mean anything, or, it probably means something different to what you think it means. All it has to do is ensure a correct result for f(1).
No, there is not 0 ways to go up 0 stairs, in the same way that 0/0 does not equal 0. It's an indeterminate result.

Get only decimal part in float number in SQL Server [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have following number in SQL Server 2008 : 5.4564556
I just want to get only 4564556 from 5.4564556
it would be better if i can get only 4 among 4564556 that is first digit of fractional part..
How can i do it?
Thanks
You can try in this way :
SELECT (5.4564556 % 1)
You can also use FLOOR():
SELECT Value - FLOOR(Value)
In your case:
5.4564556 - 5 = 0.4564556
in case of negative value you can use ABS
SELECT ABS(Value) - ABS(FLOOR(Value))
To get all the numbers behind the '.', you can (ab)use PARSENAME
select PARSENAME(5.4564556,1)
returns 4564556
Refer also to SQL - How do I get only the numbers after the decimal?
you can get first four decimal parts by :
select SUBSTRING (PARSENAME(5.4564556,1), 1, 4)
Try this
select cast(5.4564556 % 1 * 10 as int)

How should the standard factorial function behave? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I got many answers for this when I Googled "How to find factorial of a number"...
One of those examples is...
private double getFactorial(double f){
if ( f == 0 )
return 1;
return (f * getFactorial(f - 1));
}
And it works... However, the Windows Calculator surprised me: It works for decimal numbers as well!!
For example: On the Windows Calculator, the factorial of 0.5 is 0.886226925...
Is that the desired behavior? Is the factorial defined for non-integers?
Making my comment an answer:
The factorial can be generalized to nearly all numbers (real/complex/non-integral) via the Gamma Function.
The only points for which it is still undefined are for negative integers due to singularities. (This is easy to see by reversing the recursion identity for the factorial. Going from 0! to (-1)! leads to a divide by zero.)
Obviously, your code will work only for integers. For anything else it will go into an infinite recursion and cause a stackoverflow.
For integers, it's easy to compute it with a simple loop or a recursion. But for anything else, it's much harder to do.
There are two main algorithms for evaluating the factorial/Gamma Function at non-integral points:
Stirling's Formula combined with the usual recursion property.
Lanczos approximation
Wikipedia has an implementation of the latter in Python.
If you want to find the factorial of a number, it should be declared with a long/int return type and a long/int type parameter; since factorials only work for non-negative integers.
Example
private long getFactorial(int f) {
if ( f == 0 )
return 1; //0!=1
return (f * getFactorial(f - 1)); //basic recursive formula
}
Your code goes into an infinite loop (or at least a stack overflow), say, when you try to find (-1.1)! or (0.001)! ...
Also I am not sure what goes on in the windows calculator (say, 1.5! returns 1.3293403881791370204736256125059) but I think this would help: http://en.wikipedia.org/wiki/Gamma_function
Factorial can be generalized to real numbers using Gamma function. Let G(x) be gamma of x and P(x) generalized factorial of x (also known as the Pi function). The relation between G(x) and P(x) is P(x)=x*G(x).
You can find that G(0.5) is sqrt(pi)=sqrt(3.141592...). Hence P(0.5)=0.5*sqrt(3.141592...)=0.5*1.772453...=0.886226...
Note that there is no direct connection between the fact that the famous pi number happens to be part of the value of G(x) and the fact that the other function is called Pi function.
To be precise, the factorial function is a function defined exactly on all non-negative integers.
The gamma function is not the factorial function. It is a function on the complex numbers.
gamma(n) computes (n-1)! for n a positive integer.
The factorial function should behave precisely as you have designed it. No more, no less.

2D matrix of QStrings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to make an application with which you can reserve ticket for your travel. In fact, I'm designing the system for an airline. When I want to create a database (a 2D matrix that saves the number of seats in flights), it gives me errors.
The number of flights set in different place and the number is changing this is my code:
QString** matrix = new QString*[numberofFlights];
for (int i = 0; i < numberofFlight; i++)
{
matrix[i] = new QString[numberofSeats];
}
What class in Qt should i use?
A must-read: Qt container classes.
You could use QVectors or QLists or another container class. For example, to build a vector of vectors:
QVector< QVector<QString> > matrix(numberOfFlights);
for (int i=0; i<numberOfFlights; i++)
matrix[i].fill("", numberOfSeats);
This will create numberOfFlights vectors, that each contain numberOfSeats empty strings.
To set a specific seat:
matrix[flight][seat] = "whatever";
You can iterate over the vectors with the usual Qt foreach, or iterators, or plain for.

Resources