Skip to content

Commit 6e30a45

Browse files
add sieveoferatosthenes
1 parent bc07f49 commit 6e30a45

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

31Greedy/04_lc1710.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
bool cmp(vector<int> &a, vector<int>& b){
4+
return a[1]>b[1];
5+
}
6+
/*
7+
Tc - O(n log n)
8+
*/
9+
int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
10+
int n = boxTypes.size();
11+
12+
sort(boxTypes.begin(), boxTypes.end(), cmp);
13+
int profit = 0;
14+
for(int i=0;i<n;i++){
15+
if(boxTypes[i][0] <= truckSize){
16+
profit += boxTypes[i][0] * boxTypes[i][1];
17+
truckSize -= boxTypes[i][0];
18+
}else{
19+
profit += truckSize * boxTypes[i][1];
20+
truckSize = 0;
21+
break;
22+
}
23+
}
24+
return profit;
25+
}
26+
int main(){
27+
vector<vector<int>> boxTypes = {{5,10},{2,5},{4,7},{3,9}};
28+
cout<<"maximum units on truck can be: "<<maximumUnits(boxTypes, 10)<<" units";
29+
return 0;
30+
}

31Greedy/04_lc1710.exe

211 KB
Binary file not shown.

33algos/01sieveoferatosthenes.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
//find first prime
4+
// int fPrime(vector<int> &arr){
5+
// for(int i=0;i<arr.size();i++){
6+
// if (arr[i] <= 1) continue;
7+
// if(arr[i] == 2) return i;
8+
// if(arr[i] == 3) return i;
9+
// if (arr[i] % 2 == 0 || arr[i] % 3 == 0) {
10+
// continue;
11+
// }
12+
// // Iterate from 5 up to the square root of n
13+
// // We only need to check divisibility by numbers of the form 6k ± 1
14+
// for (int j = 5; j * j <= arr[i]; j = j + 6) {
15+
// if (arr[i] % j == 0 || arr[i] % (j + 2) == 0) {
16+
// continue;
17+
// }
18+
// }
19+
// return i;
20+
// }
21+
// return 0;
22+
// }
23+
// //iterate and mark all mutiples of first prime Number
24+
// void mark(vector<int> &arr, int firstidx){
25+
// //mark -1
26+
// for(int i=firstidx+1;i<arr.size();i++){
27+
// if(arr[i] % arr[firstidx] == 0){
28+
// arr[i] = -1;
29+
// }
30+
// }
31+
// }
32+
// int main(){
33+
// vector<int>arr = {2,3,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
34+
// int firstPrimeIdx = fPrime(arr);
35+
// cout<<firstPrimeIdx<<endl;
36+
// //edge case
37+
// for(int i=0;i<firstPrimeIdx;i++){
38+
// arr[i] = -1;
39+
// }
40+
// //start marking multiples
41+
// while(firstPrimeIdx<arr.size()){
42+
// if(arr[firstPrimeIdx] != -1){
43+
// mark(arr, firstPrimeIdx);
44+
// }
45+
// firstPrimeIdx++;
46+
// }
47+
// for(auto ele:arr){
48+
// cout<<ele<<" ";
49+
// }
50+
// cout<<endl;
51+
// cout<<"All prime numbers are:"<<endl;
52+
// for(auto ele:arr){
53+
// if (ele != -1) cout<<ele<<" ";
54+
// }
55+
// cout<<endl;
56+
57+
// return 0;
58+
// }
59+
60+
vector<int> sieveOfEratosthenes(int n) {
61+
vector<bool> isPrime(n + 1, true);
62+
// 0 and 1 are not prime numbers
63+
isPrime[0] = isPrime[1] = false;
64+
65+
// Iterate up to the square root of n
66+
for (int p = 2; p * p <= n; ++p) {
67+
// If isPrime[p] is not changed, then it is a prime
68+
if (isPrime[p]) {
69+
// multiples which are less than p*p are already been marked.
70+
// i = 2, 4, 6, 8, 10
71+
for (int i = p * p; i <= n; i += p) {
72+
isPrime[i] = false;
73+
}
74+
}
75+
}
76+
// Store all prime numbers in a vector
77+
vector<int> primes;
78+
for (int p = 2; p <= n; ++p) {
79+
if (isPrime[p]) {
80+
primes.push_back(p);
81+
}
82+
}
83+
return primes;
84+
}
85+
86+
int main() {
87+
int n;
88+
89+
cout << "Enter the upper limit: ";
90+
cin >> n;
91+
92+
vector<int> primeNumbers = sieveOfEratosthenes(n);
93+
94+
cout << "Prime numbers up to " << n << " are: ";
95+
for (int prime : primeNumbers) {
96+
cout << prime << " ";
97+
}
98+
cout <<endl;
99+
100+
return 0;
101+
}

33algos/01sieveoferatosthenes.exe

162 KB
Binary file not shown.

sample.exe

-2.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)