Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5fcbd3

Browse files
add algorithms
1 parent 7bae849 commit d5fcbd3

File tree

5,928 files changed

+1277177
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,928 files changed

+1277177
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Contributing Guidelines
2+
3+
## Legal
4+
5+
Contributions must be all your own work; plagiarism is not allowed.
6+
By submitting a pull request, you agree to license your contribution
7+
under the [license of this repository](./LICENSE.txt).
8+
9+
## Translations
10+
11+
Translations go in the appropriate folder named by the locale code.
12+
13+
Do not change the structure or formatting of the original (English) explanation when translating or updating translations.
14+
You may change number formatting to fit the locale
15+
(`42.33` may be formatted as `42,33` in a german translation for instance)
16+
as long as it doesn't create ambiguities
17+
(e.g. `[42,33, 13]` in an array would be disallowed and should either remain `[42.33, 13]` or use a different delimiter `[42,33; 13]`).
18+
19+
## Writing Explanations
20+
21+
See the [explanation of the Euclidean Algorithm](en/Basic%20Math/Euclidean%20algorithm.md)
22+
for an example of how a good explanation may look like.
23+
24+
### Structure
25+
26+
You should structure your explanations using headings.
27+
The top-level heading should be the name of the algorithm or data structure to be explained
28+
29+
Subsequent sub-headings *may* be:
30+
31+
1. Problem solved by the algorithm
32+
2. Design/approach of the algorithm
33+
3. Detailed (yet not too technical) description of the algorithm, usually including (pseudo)-code
34+
4. Analysis (proof of correctness, best/worst/average cases, time & space complexity)
35+
5. Walkthrough(s) of how the algorithm processes example input(s)
36+
6. Application(s) of the algorithm
37+
7. Further resources such as reference implementations, videos or other explanations. *If possible, link to The Algorithms' website as well (example: <https://the-algorithms.com/algorithm/quick-sort>).*
38+
39+
### Capitalization
40+
41+
- Use *Title Case* for headings.
42+
- Start new sentences with a capital letter.
43+
44+
### Typographical Conventions
45+
46+
- Leave a space after punctuation such as `.`, `!`, `?`, `,`, `;` or `:`.
47+
- Add a space before and after `-`. **Do not add a space before punctuation.**
48+
- Add a space before an opening parenthesis `(`. Do not add a space before the closing parenthesis `)`.
49+
- Add spaces around (but not inside) quotes. Use single quotes for quotes within quotes.
50+
51+
### Markdown Conventions
52+
53+
[GitHub-flavored Markdown is used](https://github.github.com/gfm/). Explanations should render well when viewed from GitHub.
54+
55+
- **Do not add redundant formatting.** Formatting should always be meaningful.
56+
If you apply a certain formatting to all elements of a certain kind (e.g. adding emphasis around all headings), you're doing something wrong.
57+
- Use ATX-style "hashtag" headings: `#`, `##`, `###`, `####`, `#####`, `######` rather than Setext-style "underline" headings.
58+
Leave blank lines around headings. The first heading should always be `# Title`. Subheadings must be exactly one level deeper than their parents.
59+
- Indent lists by two blanks. Prefer `-` over `*` for bulleted lists. Enumerate numbered lists correctly, starting at `1`.
60+
- Use fenced code blocks (and specify the correct language) rather than using indented code blocks.
61+
Format code inside fenced code blocks properly (prefer pseudocode over code though). Leave blank lines around fenced code blocks.
62+
- Use named links `[name](link)` rather than relying on automatic link detection or using `<>`-links.
63+
There are rarely good reasons to not give a link a descriptive name.
64+
- Use HTML only if necessary (rarely - if ever - the case). Do not use HTML for unnecessary formatting.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2020-2024 The Algorithms and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Algorithms explanation
2+
3+
[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms/community)&nbsp;
4+
[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/TheAlgorithms/Ruby/blob/master/CONTRIBUTING.md)&nbsp;
5+
![Repository size](https://img.shields.io/github/repo-size/TheAlgorithms/Algorithms-Explanation.svg?label=Repo%20size&style=flat-square)&nbsp;
6+
[![Discord chat](https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=5865F2&style=flat-square)](https://discord.gg/c7MnfGFGa6)
7+
8+
Popular algorithms explained in simple language with examples and links to their implementation in various programming languages and other required resources.
9+
10+
## Languages
11+
12+
- [Uzbek](./uz)
13+
- [Brazilian Portuguese](./pt-br)
14+
- [English](./en)
15+
- [French](./fr)
16+
- [Hebrew](./he)
17+
- [Indonesian](./id)
18+
- [Korean](./ko)
19+
- [Nepali](./ne)
20+
- [Spanish](./es)
21+
22+
To add a new language, create a new folder using 2 character `ISO 639-1` code of that language. For example use `hi` for `Hindi` explanations.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Aliquot Sum
2+
3+
The aliquot sum $s(n)$ of a positive integer $n$ is the sum of all proper divisors of $n$, that is, all divisors of $n$ other than the number $n$ itself. That is:
4+
5+
$$ s(n) = \sum_{d | n, d \neq n} {d} $$
6+
7+
So, for example, the aliquot sum of the number $15$ is $(1 + 3 + 5) = 9$
8+
9+
Aliquot sum is a very useful property in Number Theory, and can be used for defining:
10+
- Prime Numbers
11+
- Deficient Numbers
12+
- Abundant Numbers
13+
- Perfect Numbers
14+
- Amicable Numbers
15+
- Untouchable Numbers
16+
- Aliquot Sequence of a number
17+
- Quasiperfect & Almost Perfect Numbers
18+
- Sociable Numbers
19+
20+
## Facts about Aliquot Sum
21+
- 1 is the only number whose aliquot sum is 0
22+
- The aliquot sums of perfect numbers is equal to the numbers itself
23+
- For a [*semiprime*](https://en.wikipedia.org/wiki/Semiprime) number of the form $pq$, the aliquot sum is $p + q + 1$
24+
- The Aliquot sum function was one of favorite topics of investigation for the world famous Mathematician, [Paul Erdős](https://en.wikipedia.org/wiki/Paul_Erd%C5%91s)
25+
26+
## Approach on finding the Aliquot sum
27+
28+
### Step 1: *Obtain the proper divisors of the number*
29+
We loop through all the numbers from $1$ to $[\frac{n} 2]$ and check if they divide $n$, which if they do we add them as a proper divisor.
30+
31+
The reason we take the upper bound as $[\frac{n} 2]$ is that, the largest possible proper divisor of an even number is $\frac{n} 2 $, and if the number is odd, then its largest proper divisor is less than $[\frac{n} 2]$, hence making it a foolproof upper bound which is computationally less intensive than looping from $1$ to $n$.
32+
33+
### Step 2: *Add the proper divisors of the number*
34+
The sum which we obtain is the aliquot sum of the number
35+
36+
## Implementations
37+
- [TheAlgorithms](https://the-algorithms.com/algorithm/aliquot-sum)
38+
39+
## Sources
40+
- [Wikipedia](https://en.wikipedia.org/wiki/Aliquot_sum)
41+
- [GeeksForGeeks](https://www.geeksforgeeks.org/aliquot-sum/)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Arithmetic Progression
2+
3+
A sequence of numbers is said to be in an `Arithmetic progression` if the difference between any two consecutive terms is always the same. In simple terms, it means that the next number in the series is calculated by adding a fixed number to the previous number in the series.
4+
For example, 2, 4, 6, 8, 10 is an AP because the difference between any two consecutive terms in the series (common difference) is same (4 - 2 = 6 - 4 = 8 - 6 = 10 - 8 = 2).
5+
<p align="center">
6+
<img width="60%" src="https://user-images.githubusercontent.com/75872316/122635132-ce38d100-d0ff-11eb-8fdf-2e14a9f640cc.png">
7+
</p>
8+
9+
**Facts about Arithmetic Progression:**
10+
11+
1. Initial term: In an arithmetic progression, the first number in the series is called the initial term.
12+
2. Common difference: The value by which consecutive terms increase or decrease is called the `common difference`.
13+
3. The behavior of the arithmetic progression depends on the common difference `d`. If the common difference is positive, then the members (terms) will grow towards positive infinity. But if the common difference is negative, then the members (terms) will grow towards negative infinity.
14+
15+
**Formula of the nth term of an A.P:**
16+
17+
`a` is the initial term, and `d` is a common difference. Thus, the explicit formula is:
18+
<p align="center">
19+
<img width="60%" src="https://user-images.githubusercontent.com/75872316/122635193-25d73c80-d100-11eb-9015-344d36633704.png">
20+
</p>
21+
22+
**Formula of the sum of first nth term of A.P:**
23+
24+
<p align="center">
25+
<img width="60%" src="https://user-images.githubusercontent.com/75872316/122635260-7a7ab780-d100-11eb-82a5-8ceeba3aff03.png">
26+
</p>
27+
28+
**General Formulas to solve problems related to Arithmetic Progressions:**
29+
30+
If `a` is the first term and `d` too, that would be a common difference:
31+
- **nth term of an AP** = `a + (n-1)*d`.
32+
- **Arithmetic Mean** = `Sum of all terms in the AP / Number of terms in the AP`.
33+
- **Sum of ‘n’ terms** of an AP = 0.5 n (first term + last term) = `0.5 n [ 2a + (n-1) d ]`.
34+
35+
# Source
36+
37+
- [Arithmetic Progression](https://www.geeksforgeeks.org/arithmetic-progression)
38+
39+
# YouTube
40+
41+
- [Video URL for concept](https://youtu.be/gua96ju_FBk)
42+
- [Video for understanding AP Dynamic Programming in C++](https://youtu.be/U_qtSRQYoPs)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Average (Mean)
2+
3+
Calculate the average of a list of numbers using mean.
4+
5+
## Applications
6+
7+
Calculating the mean of a list of numbers is one of the most common ways to
8+
determine the average of those numbers.
9+
10+
Calculating a mean would be useful in these situations:
11+
12+
- Determining the average score for all players of a video game level.
13+
- Finding the average grade for tests that a student took this semester.
14+
- Determining the average size of all files in a directory/folder.
15+
16+
## Steps
17+
18+
1. Input a list of numbers.
19+
2. Calculate the sum of all numbers in the list.
20+
3. Count the numbers in the list.
21+
4. Divide the sum by the total count of numbers in the list.
22+
5. Return mean.
23+
24+
## Example
25+
26+
Given the list `[2, 4, 6, 8, 20, 50, 70]`, let's calculate the average.
27+
28+
### Step 1
29+
30+
Send `[2, 4, 6, 8, 20, 50, 70]` as input for a method/function.
31+
32+
### Step 2
33+
34+
Add all the numbers together.
35+
36+
`2 + 4 + 6 + 8 + 20 + 50 + 70 = 160`, so `sum = 160`.
37+
38+
### Step 3
39+
40+
Count the numbers in the list.
41+
42+
The list has seven numbers, so `count = 7`.
43+
44+
### Step 4
45+
46+
Divide the sum of all the numbers by the count of the numbers.
47+
48+
```
49+
sum = 160
50+
count = 7
51+
```
52+
If we ignore significant digits: `sum / count = `22.<u>857142</u>
53+
54+
If we properly consider significant digits: `sum / count = 23`
55+
56+
### Step 5
57+
58+
Return the value of 22.<u>857142</u> or `23`.
59+
60+
## Video URL
61+
62+
- [Mean on Khan Academy](https://www.khanacademy.org/math/cc-sixth-grade-math/cc-6th-data-statistics/mean-and-median/v/mean-median-and-mode)
63+
64+
## Others
65+
66+
- [Mean on Wikipedia](https://en.wikipedia.org/wiki/Mean)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Euclidean algorithm
2+
3+
## Problem
4+
5+
Find the Greatest Common Divisor (GCD) of two positive integers $a$ and $b$, which is defined as the largest number $g = gcd(a, b)$ such that $g$ divides both $a$ and $b$ without remainder.
6+
7+
## Idea
8+
9+
The Euclidean algorithm is based on the simple observation that the GCD of two numbers doesn't change if the smaller number is subtracted from the larger number:
10+
11+
Let $a > b$. Let $g$ be the GCD of $a$ and $b$.
12+
Then $g$ divides $a$ and $b$. Thus $g$ also divides $a - b$.
13+
14+
Let $g'$ be the GCD of $b$ and $a - b$.
15+
16+
Proof by contradiction that $g' = g$:
17+
18+
Assume $g' < g$ or $g' > g$.
19+
20+
If $g' < g$, $g'$ would not be the *greatest* common divisor,
21+
since $g$ is also a common divisor of $a - b$ and $b$.
22+
23+
If $g' > g$, $g'$ divides $b$ and $a - b$ -
24+
that is, there exist integers $n, m$
25+
such that $g'n = b$ and $g'm = a - b$.
26+
Thus $g'm = a - g'n \iff g'm + g'n = a \iff g'(m + n) = a$.
27+
This imples that $g' > g$ also divides $a$,
28+
which contradicts the initial assumption that $g$ is the GCD of $a$ and $b$.
29+
30+
## Implementation
31+
32+
To speed matters up in practice, modulo division is used instead of repeated subtractions:
33+
$b$ can be subtracted from $a$ as long as $a >= b$.
34+
After these subtractions only the remainder of $a$ when divided by $b$ remains.
35+
36+
A straightforward Lua implementation might look as follows:
37+
38+
```lua
39+
function gcd(a, b)
40+
while b ~= 0 do
41+
a, b = b, a % b
42+
end
43+
return a
44+
end
45+
```
46+
47+
note that `%` is the modulo/remainder operator;
48+
`a` is assigned to the previous value of `b`,
49+
and `b` is assigned to the previous value of `a`
50+
modulo the previous value of `b` in each step.
51+
52+
## Analysis
53+
54+
### Space Complexity
55+
56+
The space complexity can trivially be seen to be constant:
57+
Only two numbers (of assumed constant size), $a$ and $b$, need to be stored.
58+
59+
### Time Complexity
60+
61+
Each iteration of the while loop runs in constant time and at least halves $b$.
62+
Thus $O(log_2(n))$ is an upper bound for the runtime.
63+
64+
## Walkthrough
65+
66+
Finding the GCD of $a = 42$ and $b = 12$:
67+
68+
1. $42 \mod 12 = 6$
69+
2. $12 \mod 6 = 0$
70+
71+
The result is $gcd(42, 12) = 6$.
72+
73+
Finding the GCD of $a = 633$ and $b = 142$ using the Euclidean algorithm:
74+
75+
1. $633 \mod 142 = 65$
76+
2. $142 \mod 65 = 12$
77+
3. $65 \mod 12 = 5$
78+
4. $12 \mod 5 = 2$
79+
5. $5 \mod 2 = 1$
80+
6. $2 \mod 1 = 0$
81+
82+
The result is $gcd(633, 142) = 1$: $a$ and $b$ are co-prime.
83+
84+
## Applications
85+
86+
* Shortening fractions
87+
* Finding the Least Common Multiple (LCM)
88+
* Efficiently checking whether two numbers are co-prime (needed e.g. for the RSA cryptosystem)
89+
90+
## Resources
91+
92+
* [Wikipedia Article](https://en.wikipedia.org/wiki/Euclidean_algorithm)

0 commit comments

Comments
 (0)