File tree 5 files changed +85
-28
lines changed 5 files changed +85
-28
lines changed Original file line number Diff line number Diff line change @@ -35,11 +35,11 @@ void printJobScheduling(Job arr[], int n)
35
35
int profit = 0 ;
36
36
37
37
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
39
39
for (int j = min (n, arr[i].dead ) - 1 ; j >= 0 ; j--) {
40
40
// Free slot found
41
41
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;
43
43
result[j] = i; // Add this job to result
44
44
slot[j] = true ; // Make this slot occupied
45
45
profit += arr[i].profit ; // Add profit of current job to result
@@ -77,11 +77,12 @@ void printJobScheduling(Job arr[], int n)
77
77
78
78
int main ()
79
79
{
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 } };
85
86
86
87
int n = sizeof (arr) / sizeof (arr[0 ]);
87
88
cout << " Following is maximum profit sequence of jobs "
Original file line number Diff line number Diff line change 3
3
4
4
// Function to print all prime factors of a given number
5
5
void primeFactors (int n) {
6
- // Print the number of 2s that divide n
7
6
while (n % 2 == 0 ) {
8
- std:: cout << 2 << " " ;
7
+ cout << 2 << " " ;
9
8
n = n / 2 ;
10
9
}
11
10
12
- // n must be odd at this point. So we can skip one element (i = i + 2)
13
11
for (int i = 3 ; i * i <= n; i = i + 2 ) {
14
- // While i divides n, print i and divide n
15
12
while (n % i == 0 ) {
16
13
cout << i << " " ;
17
14
n = n / i;
18
15
}
19
16
}
20
17
21
- // This condition is to handle the case when n is a prime number greater than 2
22
18
if (n > 2 )
23
- std:: cout << n << " " ;
19
+ cout << n << " " ;
24
20
}
25
21
26
22
int main () {
27
23
int n = 315 ;
28
24
29
- std:: cout << " Prime factors of " << n << " : " ;
25
+ cout << " Prime factors of " << n << " : " ;
30
26
primeFactors (n);
31
27
32
28
return 0 ;
Original file line number Diff line number Diff line change 3
3
4
4
// Function to print all prime numbers smaller than or equal to n
5
5
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 );
10
7
11
8
for (int p = 2 ; p * p <= n; p++) {
12
- // If prime[p] is not changed, then it is a prime
13
9
if (prime[p] == true ) {
14
- // Update all multiples of p
15
10
for (int i = p * p; i <= n; i += p)
16
11
prime[i] = false ;
17
12
}
18
13
}
19
-
20
- // Print all prime numbers
21
14
for (int p = 2 ; p <= n; p++)
22
15
if (prime[p])
23
- std:: cout << p << " " ;
16
+ cout << p << " " ;
24
17
}
25
18
26
19
int main () {
27
20
int n = 10 ;
28
21
29
- std:: cout << " Prime numbers smaller than or equal to " << n << " : " ;
22
+ cout << " Prime numbers smaller than or equal to " << n << " : " ;
30
23
sieveOfEratosthenes (n);
31
24
32
25
return 0 ;
Original file line number Diff line number Diff line change @@ -18,9 +18,8 @@ bool isPrime(int n) {
18
18
return true ;
19
19
}
20
20
21
- // Function to find the highest occurring digit in prime numbers in the range L to R
22
21
int highestOccurringDigit (int L, int R) {
23
- std:: vector<int > digitFreq (10 , 0 );
22
+ vector<int > digitFreq (10 , 0 );
24
23
25
24
for (int i = L; i <= R; ++i) {
26
25
if (isPrime (i)) {
@@ -49,9 +48,9 @@ int main() {
49
48
int result = highestOccurringDigit (L, R);
50
49
51
50
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;
53
52
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;
55
54
56
55
return 0 ;
57
56
}
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments