-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2105 from kodenook/develop
#6- php, python, ruby, javascript, typescript
- Loading branch information
Showing
5 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
/** | ||
* The function "recursion" recursively logs numbers starting from the input number until reaching 0. | ||
* @param number - The `recursion` function takes a parameter `number`, which is used to control the | ||
* recursion. The function will log the value of `number` and then recursively call itself with | ||
* `number` decremented by 1, until `number` is no longer greater than 0. | ||
*/ | ||
function recursion(number) { | ||
console.log(number) | ||
if (number > 0) { | ||
recursion(--number) | ||
} | ||
} | ||
|
||
recursion(100) | ||
|
||
/* | ||
Exercise | ||
*/ | ||
|
||
/** | ||
* The factorial function calculates the factorial of a given number recursively. | ||
* @param number - The `number` parameter in the `factorial` function represents the integer for which | ||
* you want to calculate the factorial. It is the input value for which the factorial will be computed. | ||
* @param [result=1] - The `result` parameter in the `factorial` function is used to keep track of the | ||
* intermediate result as the factorial calculation progresses through recursive calls. It starts with | ||
* a default value of 1 and gets updated with the multiplication of `number` and the current `result` | ||
* in each recursive call. | ||
*/ | ||
function factorial(number, result = 1) { | ||
|
||
if (number > 1) { | ||
factorial(number - 1, number * result) | ||
} else { | ||
console.log(result) | ||
} | ||
} | ||
|
||
factorial(5) | ||
|
||
/** | ||
* The function calculates the Fibonacci number at a given position using recursion. | ||
* @param position - The `position` parameter in the `fibonacci` function represents the position of | ||
* the Fibonacci number in the sequence that you want to calculate. For example, if `position` is 5, | ||
* the function will return the 5th Fibonacci number in the sequence. | ||
* @returns the value of the Fibonacci sequence at the specified position. | ||
*/ | ||
function fibonacci(position) { | ||
if (position > 2) { | ||
return fibonacci(position - 1) + fibonacci(position - 2) | ||
} else { | ||
return 1 | ||
} | ||
} | ||
|
||
console.log(fibonacci(7)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* The above PHP function uses recursion to print numbers from the input number down to 0. | ||
* | ||
* @param int number The function `recursion` takes an integer parameter `number` and recursively | ||
* prints the value of `number` until it reaches 0. Each time it prints the value of `number`, it | ||
* decrements the value by 1 before calling itself recursively. | ||
*/ | ||
function recursion(int $number): void | ||
{ | ||
echo $number, PHP_EOL; | ||
if ($number > 0) recursion(--$number); | ||
} | ||
|
||
recursion(100); | ||
|
||
/* | ||
Exercise | ||
*/ | ||
|
||
/** | ||
* The function calculates the factorial of a given number recursively in PHP. | ||
* | ||
* @param int number The `number` parameter in the `factorial` function represents the integer for | ||
* which you want to calculate the factorial. The factorial of a non-negative integer n is the product | ||
* of all positive integers less than or equal to n. | ||
* @param int result The `result` parameter in the `factorial` function is used to keep track of the | ||
* intermediate result as the factorial calculation progresses through recursive calls. It starts with | ||
* a default value of 1 and gets updated with the multiplication of the current number and the previous | ||
* result in each recursive call. | ||
*/ | ||
function factorial(int $number, int $result = 1): void | ||
{ | ||
if ($number > 1) factorial($number - 1, $number * $result); | ||
else echo $result, PHP_EOL; | ||
} | ||
|
||
factorial(5); | ||
|
||
/** | ||
* The function calculates the Fibonacci sequence up to a given number using recursion in PHP. | ||
* | ||
* @param int number The function you provided is a recursive implementation of the Fibonacci sequence. | ||
* It calculates the Fibonacci number at a given position in the sequence. | ||
* @return int the nth number in the Fibonacci sequence, where n is the input number provided to the | ||
* function. | ||
*/ | ||
function fibonacci(int $number): int | ||
{ | ||
if ($number > 2) return fibonacci($number - 1) + fibonacci($number - 2); | ||
else return 1; | ||
|
||
} | ||
|
||
echo fibonacci(7); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
def recursion(number: int) -> None: | ||
""" | ||
The function `recursion` in Python prints numbers from the input number down to 0 using recursion. | ||
:param number: The `recursion` function takes an integer `number` as a parameter. The function will | ||
print the value of `number` and then recursively call itself with `number-1` until `number` is | ||
greater than 0 | ||
:type number: int | ||
""" | ||
print(f'{number}') | ||
if number > 0 : | ||
recursion(number-1) | ||
|
||
recursion(100) | ||
|
||
""" | ||
Exercise | ||
""" | ||
|
||
def factorial(number: int, result: int = 1) -> None: | ||
""" | ||
The function calculates the factorial of a given number recursively. | ||
:param number: The `number` parameter in the `factorial` function represents the integer for which | ||
you want to calculate the factorial. It is the input number for which the factorial will be computed | ||
:type number: int | ||
:param result: The `result` parameter in the `factorial` function is used to keep track of the | ||
intermediate result of the factorial calculation as the function recursively calls itself. It starts | ||
with a default value of 1 and gets updated as the function progresses through the recursive calls, | ||
defaults to 1 | ||
:type result: int (optional) | ||
""" | ||
if number > 1 : | ||
factorial(number-1, result*number) | ||
else: | ||
print(f'{result}') | ||
|
||
factorial(5) | ||
|
||
def fibonacci(position: int) -> int: | ||
""" | ||
This Python function calculates the Fibonacci number at a given position using recursion. | ||
:param position: The `position` parameter in the `fibonacci` function represents the position of the | ||
Fibonacci number in the sequence that you want to calculate. For example, if `position` is 5, the | ||
function will return the 5th Fibonacci number in the sequence | ||
:type position: int | ||
:return: the value of the Fibonacci sequence at the specified position. | ||
""" | ||
if position > 2: | ||
return fibonacci(position-1)+fibonacci(position-2) | ||
else: | ||
return 1 | ||
|
||
print(fibonacci(7)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
## | ||
# The `recursion` function in Ruby recursively prints numbers from the input number down to 0. | ||
# | ||
# Args: | ||
# number: The `recursion` method takes a parameter `number`, which is used to control the recursion. | ||
# The method will output the value of `number` and then call itself recursively with `number-1` until | ||
# `number` is less than or equal to 0. | ||
def recursion(number) | ||
puts number | ||
recursion(number-1) if number > 0 | ||
end | ||
|
||
recursion(100) | ||
|
||
=begin | ||
Exercise | ||
=end | ||
|
||
## | ||
# The above Ruby function calculates the factorial of a given number recursively. | ||
# | ||
# Args: | ||
# number: The `number` parameter in the `factorial` function represents the integer for which you | ||
# want to calculate the factorial. The factorial of a non-negative integer `n`, denoted as `n!`, is | ||
# the product of all positive integers less than or equal to `n`. | ||
# resultado: The `resultado` parameter in the `factorial` function is used to keep track of the | ||
# intermediate result as the factorial calculation progresses through recursive calls. It starts with | ||
# a default value of 1 and gets updated with the multiplication of the current number and the previous | ||
# result in each recursive call. Defaults to 1 | ||
def factorial(number, resultado = 1) | ||
if number > 1 | ||
factorial(number-1, number*resultado) | ||
else | ||
puts resultado | ||
end | ||
end | ||
|
||
factorial(5) | ||
|
||
## | ||
# The above Ruby function calculates the Fibonacci number at a given position using recursion. | ||
# | ||
# Args: | ||
# position: The `fibonacci` function you provided calculates the Fibonacci number at a given | ||
# position in the sequence. The Fibonacci sequence starts with 1, 1, and each subsequent number is the | ||
# sum of the two preceding numbers. | ||
# | ||
# Returns: | ||
# The code is a recursive function to calculate the Fibonacci number at a given position. If the | ||
# position is greater than 2, it recursively calls itself to calculate the Fibonacci number by adding | ||
# the previous two Fibonacci numbers. If the position is 2 or less, it returns 1. | ||
def fibonacci(position) | ||
if position > 2 | ||
return fibonacci(position - 1) + fibonacci(position - 2) | ||
else | ||
return 1 | ||
end | ||
end | ||
|
||
puts fibonacci(7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
/** | ||
* The function "recursion" in TypeScript prints numbers from the input number down to 0 using | ||
* recursion. | ||
* @param {number} number - The `number` parameter in the `recursion` function is a numeric value that | ||
* determines how many times the function will recursively call itself. The function will log the value | ||
* of `number` to the console and then recursively call itself with `number - 1` until `number` is | ||
* greater than | ||
*/ | ||
function recursion(number: number): void { | ||
console.log(number) | ||
if (number > 0) { | ||
recursion(--number) | ||
} | ||
} | ||
|
||
recursion(100) | ||
|
||
/* | ||
Exercise | ||
*/ | ||
|
||
/** | ||
* The function calculates the factorial of a given number recursively in TypeScript. | ||
* @param {number} number - The `number` parameter in the `factorial` function represents the number | ||
* for which you want to calculate the factorial. It is the input value for which the factorial will be | ||
* calculated recursively. | ||
* @param {number} [result=1] - The `result` parameter in the `factorial` function is used to keep | ||
* track of the intermediate result as the factorial calculation progresses through recursive calls. It | ||
* is initialized with a default value of 1 when the function is first called. As the function | ||
* recursively calculates the factorial of a number, the ` | ||
*/ | ||
function factorial(number: number, result: number = 1): void { | ||
|
||
if (number > 1) { | ||
factorial(number - 1, number * result) | ||
} else { | ||
console.log(result) | ||
} | ||
} | ||
|
||
factorial(5) | ||
|
||
/** | ||
* The function calculates the Fibonacci number at a given position using recursion. | ||
* @param {number} position - The `position` parameter in the `fibonacci` function represents the | ||
* position of the Fibonacci number in the sequence that you want to calculate. For example, if | ||
* `position` is 5, it means you want to find the 5th Fibonacci number in the sequence. | ||
* @returns the value of the Fibonacci sequence at the specified position. | ||
*/ | ||
function fibonacci(position: number): number { | ||
if (position > 2) { | ||
return fibonacci(position - 1) + fibonacci(position - 2) | ||
} else { | ||
return 1 | ||
} | ||
} | ||
|
||
console.log(fibonacci(7)) |