Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hacktober Fest #1

Open
wants to merge 6 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
29 changes: 29 additions & 0 deletions c++/LCS/LCS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<iostream>
#include<fstream>
#include<string>
#include"LongestCommonSubsequence.hh"

using namespace std;

int main () {

int m,n;

// Reading Text files
string mytext, mysecondtext;
ifstream myfile("text1.txt");
getline(myfile,mytext);

ifstream myfile2("text2.txt");
getline(myfile2,mysecondtext);

m = int(mytext.length());
n = int(mysecondtext.length());
cout << "String 1: " << m << endl;
cout << "String 2: " << n << endl;

cout <<"LCS: "<< LCS_V2(mytext.c_str(), mysecondtext.c_str(), m, n) << endl;
cout <<"Print LCS: " << LCS_Common(mytext.c_str(), mysecondtext.c_str(), m, n) << endl;

return 0;
}
87 changes: 87 additions & 0 deletions c++/LCS/LongestCommonSubsequence.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Example: Longest Common Subsequence calculates the number of letters
in common between two strings.

This example is terrible because it is O(2**n). The key is to use
dynamic programming for this problem or more efficiently, to create
the array and fill it sequentially since it all must be done in any
case. This placeholder is an example of how to contribute an
algorithm, as a header file with a separate test code
that demonstrates its use.

*/
int LCS(const char* a, const char* b) {
if (*a == '\0' || *b == '\0')
return 0;
if (*a == *b)
return 1 + LCS(a+1,b+1);
return std::max(LCS(a+1,b), LCS(a, b+1));
}

int LCS_V2(const char a[], const char b[], int m, int n) {
int memo[m+1][n+1];
int i, j;
for (i = 0; i < m; i++){ //0(m)
memo[i][0] = 0;
}
for (j = 0; j < n; j++){ //O(n)
memo[0][j] = 0;
}
for (i = 1; i < m + 1 ; i++){
for (j = 1; j < n + 1; j++){
if (a[i] == b[j]){
memo[i][j] = 1 + memo[i-1][j-1];
}
else {
memo[i][j] = std::max(memo[i][j-1], memo[i-1][j]);
}
}
}
return memo[m][n];
}

int LCS_Common(const char a[], const char b[], int m, int n){
int memo2[m+1][n+1];
int i, j;
for (i = 0; i < m; i++){ //0(m)
memo2[i][0] = 0;
}
for (j = 0; j < n; j++){ //O(n)
memo2[0][j] = 0;
}
for (i = 1; i < m + 1 ; i++){
for (j = 1; j < n + 1; j++){
if (a[i] == b[j]){
memo2[i][j] = 1 + memo2[i-1][j-1];
}
else {
memo2[i][j] = std::max(memo2[i][j-1], memo2[i-1][j]);
}
}
}

int index = memo2[m][n];
char lcs[index+1];
lcs[index] = '\0';

int x, y;
x = m;
y = n;
while (x > 0 && y > 0)
{
if (a[x-1] == b[y-1])
{
lcs[index-1] = a[x-1];
x--;
y--;
index--;
}
else if (memo2[x-1][y] > memo2[x][y-1])
x--;
else
y--;
}
std::cout << lcs << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions c++/LCS/text1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
1 change: 1 addition & 0 deletions c++/LCS/text2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
20 changes: 0 additions & 20 deletions c++/LongestCommonSubsequence.hh

This file was deleted.

79 changes: 79 additions & 0 deletions c++/NumberTheoreticAlgorithms/algorithms.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// GCD: greatest common divisor
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;

if (a > b)
return gcd(a - b, b);

return gcd(a, b - a);
}

// LCM: lowest common multiple
int lcm(int a, int b)
{
if (a == 0 && b == 0)
{
std::cout << "A and B can't be both zero for this LCM algorithem.";
return 0;
}
else
{
std::cout << (a * b) / gcd(a, b);
return 0;
}

}

// Power
int power(int x, int y)
{
int prod = 1;
while (y > 0){
if (y & 1){
prod = prod * x;
}
x = x * x;
y = y/2;
}
return prod;
}

/*
Powermod: Modular exponentation
PowerMod[a,b,m] gives the remainder of a^b divided by m.
*/
int powermod(int a, int b, int m) //O(log n)
{
int prod = 1;
while (b > 0){
if (b % 2 != 0){
prod = (prod * a) % m;
}
a = a * a % m;
b = b/2;
}
return prod;
}

// Fermat
int fermat(int n, int k) {
return powermod(n, k-2, k);
}

bool fermat2(int n, int k) {
for (int i = 0; i < k; i++) {
int a = rand() % n+1;

if (powermod(a, n-1, n) != 1){
return false; //not prime
}
}
return true; //prime
}

27 changes: 27 additions & 0 deletions c++/NumberTheoreticAlgorithms/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include "algorithms.hh"
using namespace std;

int main(int argc, char *argv[]){

int a[argc];

cout<<"You have entered: "<<argc-1<<" numbers"<<'\n';
for (int i = 0; i<argc; i++){
a[i] = atoi(argv[i]);
}

cout<<"GCD: "<< gcd(a[1], a[2]) << endl;
cout<<"LCM: "<< lcm(a[1], a[2]) << endl;
cout <<"Power: " << power(a[1], a[2])<< endl;
cout<<"PowerMod: "<< powermod(a[1], a[2], a[3]) << endl;
cout <<"Fermat: " << fermat(a[1], a[2])<< endl;

if (fermat2(a[1], a[2]) == 0){
cout << "Not prime." << endl;
}
else {
cout << "Is prime." <<endl;
}

}
71 changes: 71 additions & 0 deletions c++/Primes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

bool isPrime(int n) { //O(sqrt n)
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false; // not prime
}
return true; // prime
}

void Erathosthenes(int num){
int count = 0;
bool* sieve = new bool [num];

for (int i = 2; i <= num; i++){ //O(num)
sieve[i] = 1;
}

// remove even
for (int j = 4; j <=num; j+=2){ //O(num)
sieve[j] = 0;
}

for (int k = 3; k <= num; k += 2){ //O(num)
if (sieve[k] == 1){
for (int l = k*k; l <= num; l = l + 2*k){
sieve[l] = 0;
}
}
}

vector <int> list;
// counter of primes
for(int m = 2; m <=num; m++){
if (sieve[m] == 1){
list.push_back(m);
count++;
}
}
cout << count << "\n" << "List of Primes: ";

// if (list.empty()){
// cout << "No Primes.";
// }

for (int n = 0; n <list.size(); n++){
cout << list.at(n) << " ";
}

cout << endl;
}

int main() {

int N;
cout << "Give me any number: ";
cin >> N;

if (isPrime(N) == 0){
cout << "Number of prime: ";
Erathosthenes(N);
}
else {
cout << N << " is prime." <<endl;
}

return 0;
}
Loading