Skip to content

Commit 684c4f8

Browse files
committed
Number Theory
1 parent a866e29 commit 684c4f8

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

Algorithms/04_Greedy/04_JobSequencing.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ void printJobScheduling(Job arr[], int n)
3535
int profit = 0;
3636

3737
for (int i = 0; i < n; i++) {
38-
cout << "Last possible slot for job " << arr[i].id << " is: " << min(n, arr[i].dead)-1 << endl;// Last possible slot for job the job i
38+
cout << "Last possible slot for job " << arr[i].id << " is: " << min(n, arr[i].dead) << endl;// Last possible slot for job the job i
3939
for (int j = min(n, arr[i].dead) - 1; j >= 0; j--) {
4040
// Free slot found
4141
if (slot[j] == false) {
42-
cout << "Slot " << j << " is free for job " << arr[i].id << endl;
42+
cout << "Slot " << j + 1<< " is free for job " << arr[i].id<< endl;
4343
result[j] = i; // Add this job to result
4444
slot[j] = true; // Make this slot occupied
4545
profit += arr[i].profit; // Add profit of current job to result
@@ -77,11 +77,12 @@ void printJobScheduling(Job arr[], int n)
7777

7878
int main()
7979
{
80-
Job arr[] = { { 'a', 2, 100 },
81-
{ 'b', 1, 19 },
82-
{ 'c', 2, 27 },
83-
{ 'd', 1, 25 },
84-
{ 'e', 3, 15 } };
80+
Job arr[] = { { 'a', 5, 200 },
81+
{ 'b', 3, 180 },
82+
{ 'c', 3, 190 },
83+
{ 'd', 2, 300 },
84+
{ 'e', 4, 120 },
85+
{ 'f', 2, 100 } };
8586

8687
int n = sizeof(arr) / sizeof(arr[0]);
8788
cout << "Following is maximum profit sequence of jobs "

Algorithms/05_Number_Theory/02_Prime_Factor.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,26 @@
33

44
// Function to print all prime factors of a given number
55
void primeFactors(int n) {
6-
// Print the number of 2s that divide n
76
while (n % 2 == 0) {
8-
std::cout << 2 << " ";
7+
cout << 2 << " ";
98
n = n / 2;
109
}
1110

12-
// n must be odd at this point. So we can skip one element (i = i + 2)
1311
for (int i = 3; i * i <= n; i = i + 2) {
14-
// While i divides n, print i and divide n
1512
while (n % i == 0) {
1613
cout << i << " ";
1714
n = n / i;
1815
}
1916
}
2017

21-
// This condition is to handle the case when n is a prime number greater than 2
2218
if (n > 2)
23-
std::cout << n << " ";
19+
cout << n << " ";
2420
}
2521

2622
int main() {
2723
int n = 315;
2824

29-
std::cout << "Prime factors of " << n << ": ";
25+
cout << "Prime factors of " << n << ": ";
3026
primeFactors(n);
3127

3228
return 0;

Algorithms/05_Number_Theory/03_Prime_Sieve.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,23 @@
33

44
// Function to print all prime numbers smaller than or equal to n
55
void sieveOfEratosthenes(int n) {
6-
// Create a boolean array "prime[0..n]" and initialize
7-
// all entries as true. A value in prime[i] will
8-
// finally be false if i is Not a prime, else true.
9-
std::vector<bool> prime(n + 1, true);
6+
vector<bool> prime(n + 1, true);
107

118
for (int p = 2; p * p <= n; p++) {
12-
// If prime[p] is not changed, then it is a prime
139
if (prime[p] == true) {
14-
// Update all multiples of p
1510
for (int i = p * p; i <= n; i += p)
1611
prime[i] = false;
1712
}
1813
}
19-
20-
// Print all prime numbers
2114
for (int p = 2; p <= n; p++)
2215
if (prime[p])
23-
std::cout << p << " ";
16+
cout << p << " ";
2417
}
2518

2619
int main() {
2720
int n = 10;
2821

29-
std::cout << "Prime numbers smaller than or equal to " << n << ": ";
22+
cout << "Prime numbers smaller than or equal to " << n << ": ";
3023
sieveOfEratosthenes(n);
3124

3225
return 0;

Algorithms/05_Number_Theory/04_Prime_Range.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ bool isPrime(int n) {
1818
return true;
1919
}
2020

21-
// Function to find the highest occurring digit in prime numbers in the range L to R
2221
int highestOccurringDigit(int L, int R) {
23-
std::vector<int> digitFreq(10, 0);
22+
vector<int> digitFreq(10, 0);
2423

2524
for (int i = L; i <= R; ++i) {
2625
if (isPrime(i)) {
@@ -49,9 +48,9 @@ int main() {
4948
int result = highestOccurringDigit(L, R);
5049

5150
if (result == -1)
52-
std::cout << "No prime numbers found between " << L << " and " << R << std::endl;
51+
cout << "No prime numbers found between " << L << " and " << R << endl;
5352
else
54-
std::cout << "Highest occurring digit in prime numbers between " << L << " and " << R << " is: " << result << std::endl;
53+
cout << "Highest occurring digit in prime numbers between " << L << " and " << R << " is: " << result << endl;
5554

5655
return 0;
5756
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
1. Computing the nth Fibonacci number:
2+
3+
```
4+
function fibonacci(n):
5+
if n <= 1:
6+
return n
7+
else:
8+
a = fibonacci(n - 1)
9+
b = fibonacci(n - 2)
10+
return a + b
11+
```
12+
13+
2. Computing the factorial of a number:
14+
15+
```
16+
function factorial(n):
17+
if n <= 1:
18+
return 1
19+
else:
20+
return n * factorial(n - 1)
21+
```
22+
23+
3. Computing the square root of a number:
24+
25+
```
26+
function squareRoot(x):
27+
return squareRootHelper(x, 0, x)
28+
29+
function squareRootHelper(x, low, high):
30+
mid = (low + high) / 2
31+
square = mid * mid
32+
33+
if abs(square - x) < epsilon:
34+
return mid
35+
elif square < x:
36+
return squareRootHelper(x, mid, high)
37+
else:
38+
return squareRootHelper(x, low, mid)
39+
```
40+
41+
4. Computing the prime factorization of a number:
42+
43+
```
44+
function primeFactorization(n):
45+
for each prime p:
46+
if n is divisible by p:
47+
return p, primeFactorization(n / p)
48+
return n
49+
```
50+
51+
5. Computing the greatest common divisor (GCD) of two numbers:
52+
53+
```
54+
function gcd(a, b):
55+
if b == 0:
56+
return a
57+
else:
58+
return gcd(b, a % b)
59+
```
60+
61+
6. Computing the least common multiple (LCM) of two numbers:
62+
63+
```
64+
function lcm(a, b):
65+
return (a * b) / gcd(a, b)
66+
```
67+
68+
These pseudocode snippets illustrate the divide and conquer approach for each of the mentioned problems.

0 commit comments

Comments
 (0)