forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 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,37 @@ | ||
# Challenge: Triangular Sequence Formula | ||
|
||
## Description | ||
|
||
Write a function that defines the triangular number sequence | ||
with a formula | ||
and input ability in order to insert several different | ||
integer inputs | ||
|
||
The triangle sequence goes as follows: | ||
|
||
```python | ||
1, 3, 6, 10, 15 | ||
``` | ||
|
||
It is a formula of ***n(n+1)/2*** where ***n*** | ||
is any given number in the sequence (easier read as {1+2+3+4...}) | ||
If you wanted to check for the 5th number of the sequence, | ||
It would look like 5(5+1)/2 = 15 | ||
*Ex. Code* | ||
|
||
```python | ||
triangle(1) ➞ 1 | ||
|
||
triangle(6) ➞ 21 | ||
|
||
triangle(215) ➞ 23220 | ||
``` | ||
|
||
### Testing | ||
|
||
- Use assertion tests to check for integer inputs to | ||
ensure the right output is given | ||
|
||
### Helpful links | ||
|
||
- [Triangular Number Sequence Formula](https://www.mathsisfun.com/algebra/triangular-numbers.html) |
Empty file.
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,41 @@ | ||
""" | ||
Module for the Triangular number sequence and a code that can calculate it. | ||
Contents: | ||
- A formula that establishes the triangular number sequence n(n+1)/2 | ||
- Code for the formula to be able to take input | ||
Created on 2025-01-03 | ||
Author: Adolfo Fumero | ||
""" | ||
|
||
|
||
def triangle_side(n): | ||
""" | ||
Calculate the nth triangular number. | ||
Uses a formula of n(n+1)/2 to calculate the number of sides of the triangle in the sequence | ||
Parameters: | ||
n (int): The position in the triangular number sequence. | ||
Returns: | ||
int: The nth triangular number. | ||
Raises: | ||
If any value below 0 is input, it will throw an Error message | ||
If any non-integer value is input also, same output will occur | ||
""" | ||
if n < 1: | ||
return "Input must be a positive integer." | ||
return n * (n + 1) // 2 | ||
|
||
|
||
# Example usage | ||
try: | ||
user_input = int( | ||
input("Enter an integer value for the triangular number sequence: ") | ||
) | ||
result = triangle_side(user_input) | ||
print(f"The {user_input}th triangular number is: {result}") | ||
except ValueError: | ||
print("Please enter a valid integer.") |