Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

added sieve_of_eratosthenes #261

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions C/0-1_knapsack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include<stdio.h>

int max(int a,int b) // function to find the maximum value
{
if(b>a)
return b;
return a;
}

int main()
{
printf("Enter the size of array:\n");
int n;
scanf("%d",&n);

int w[n+1]; //weight array
printf("Enter the weights of elements\n");
for(int i = 1; i <= n; i++)
{
scanf("%d",&w[i]);
}
int val[n+1]; // value array
printf("Enter the value of elements\n");
for(int i = 1; i <= n; i++)
{
scanf("%d",&val[i]);
}

int weight; // weight of knapsack
printf("Enter the weight of knapsack\n");
scanf("%d",&weight);

int dp[n+1][weight+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=weight;j++)
{
if(i==0||j==0)
dp[i][j]=0; // initializing dp array
}
}

for(int i=1;i<=n;i++)
{
for(int j=1;j<=weight;j++)
{
if(j>=w[i])
dp[i][j]=max(val[i]+dp[i-1][j-w[i]],dp[i-1][j]);
else
dp[i][j]=dp[i-1][j];
}
}
printf("%d",dp[n][weight]); // printing max value

return 0;
}
/*
Input: size of array, weight of elements, value of elements, weight of knapsack

Output: Maximum value of knapsack

time complexity: O(n*wg),
where n is number of elements and wg is weight of knapsack


Sample input:
4
[3, 2, 4, 1]
[100, 20, 60, 40]
5

Output: 140

*/
2 changes: 2 additions & 0 deletions C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Format: -[Program name](name of the file)
[Tree Traversals](Tree_traversals.c)

[Fibonacci_series](fibonacci_series_code.c)

[0/1 Knapsack](0-1_knapsack.c)
2 changes: 2 additions & 0 deletions CPP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ Format: -[Program name](name of the file)
[N-Queens Proplem](N-QueensProblem.cpp)

[Topological Sorting](Topological_sorting.cpp)

[sieve of eratosthenes](sieve_of_eratosthenes.cpp)
34 changes: 34 additions & 0 deletions CPP/sieve_of_eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
cout << "Enter the number:" << endl;
int n;
cin >> n;
// marking prime number as zero and non-prime as 1.

int a[n+1] = {0}; // initially assuming all numbers to be prime
a[0] = 1, a[1] = 1;

vector <int> primes;

for(long long i = 2; i <= n; i++)
{
if(a[i] == 0)
{
for(long long j =i * i; j <= n;j += i) // iterating over all the multiples.
a[j] = 1; // marking non-prime number as 1
}
}

for(int i = 1; i <= n; i++)
{
if(a[i] == 0)
primes.push_back(i); //pushing all the primes in the primes vector.
}

for(int i=0;i<primes.size();i++)
cout << primes[i] << " ";

}