Skip to content

Commit

Permalink
Merge pull request #1 from VAR-solutions/master
Browse files Browse the repository at this point in the history
updated
  • Loading branch information
i-vishi committed Oct 2, 2018
2 parents 06bc7df + 1ced910 commit ef36742
Show file tree
Hide file tree
Showing 31 changed files with 1,896 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Bit Manipulation/Count-set-bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
using namespace std;

int main() {
cout << __builtin_popcount (4) << "\n";
return 0;
}
15 changes: 15 additions & 0 deletions Bit Manipulation/Swap_two_nibbles_in_a_byte.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

unsigned char swapNibbles(unsigned char x)
{
return ( (x & 0x0F)<<4 | (x & 0xF0)>>4 );
}

int main()
{
unsigned char x;
printf("Enter Character");
scanf("%c",x);
printf("%u", swapNibbles(x));
return 0;
}
31 changes: 31 additions & 0 deletions Dynamic Programming/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

//Fibonacci Series using Dynamic Programming
#include<stdio.h>

int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n+2]; // 1 extra to handle case, n = 0
int i;

/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;

for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}

return f[n];
}

int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
48 changes: 48 additions & 0 deletions Mathematics/CommonSubSeq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/* Implementation of LCS problem using DP*/
#include<iostream>
#include<cstring>
#include<cstdlib>

using namespace std;

void lcs(string X, string Y){
int m = X.length();
int n = Y.length();
int L[m+1][n+1];
for (int i=0; i<=m; i++){
for (int j=0; j<=n; j++){
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
int index = L[m][n];
char lcs[index+1];
lcs[index] = ' ';
int i = m, j = n;
while (i > 0 && j > 0){
if (X[i-1] == Y[j-1]){
lcs[index-1] = X[i-1];
i--;
j--;
index--;
}
else if (L[i-1][j] > L[i][j-1])
i--;
else
j--;
}
cout << "LCS of " << X << " and " << Y << " is " << lcs;
}

int main(){
string a,b;
cin>>a;
cin>>b;
lcs(a,b);
return 0;
}
File renamed without changes.
19 changes: 19 additions & 0 deletions Mathematics/Harmonic-Mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Implementation of compution
# of arithmetic mean, geometric mean
# and harmonic mean
# contributed by Nikita Tiwari
import math

# Function to calculate arithmetic
# mean, geometric mean and harmonic mean
def compute( a, b) :
AM = (a + b) / 2
GM = math.sqrt(a * b)
HM = (GM * GM) / AM
return HM

a = int(input())
b = int(input())
HM = compute(a, b)
print("Harmonic Mean between " , a,
" and ", b , " is " , HM )
24 changes: 24 additions & 0 deletions Mathematics/Sieve-of-Eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;

#define MAX 1000

void Sieve() {
bool prime[MAX+1];
memset(prime, 1, sizeof(prime));
for (int p = 2; p*p <= n; p++)
{
if (prime[p] == true) {
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
Sieve();
return 0;
}
18 changes: 18 additions & 0 deletions Mathematics/calc_log_2-n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <cmath>
#define lg2 log (2)
using namespace std;

double getLOG(double n){
double lgn = log (n);
double result = lgn / lg2;
return result;
}

int main(){
double n;
cin>>n;
double result = getLOG(n);
cout<<result<<endl;
return 0;
}
76 changes: 76 additions & 0 deletions Mathematics/find_hoaxnumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# code to check if a number is a hoax
# number or not.
# contributed by Manish Shaw


import math

# Function to find distinct prime factors
# of given number n
def primeFactors(n) :
res = []
if (n % 2 == 0) :
while (n % 2 == 0) :
n = int(n / 2)
res.append(2)

# n is odd at this point, since it is no
# longer divisible by 2. So we can test
# only for the odd numbers, whether they
# are factors of n
for i in range(3,int(math.sqrt(n)),2):
# Check if i is prime factor
if (n % i == 0) :
while (n % i == 0) :
n = int(n / i)
res.append(i)

# This condition is to handle the case
# when n is a prime number greater than 2
if (n > 2) :
res.append(n)
return res

# Function to calculate sum of digits of
# distinct prime factors of given number n
# and sum of digits of number n and compare
# the sums obtained
def isHoax(n) :
# Distinct prime factors of n are being
# stored in vector pf
pf = primeFactors(n)

# If n is a prime number, it cannot be a
# hoax number
if (pf[0] == n) :
return False

# Finding sum of digits of distinct prime
# factors of the number n
all_pf_sum = 0
for i in range(0,len(pf)):

# Finding sum of digits in current
# prime factor pf[i].
pf_sum = 0
while (pf[i] > 0):
pf_sum += pf[i] % 10
pf[i] = int(pf[i] / 10)

all_pf_sum += pf_sum


# Finding sum of digits of number n
sum_n = 0;
while (n > 0):
sum_n += n % 10
n = int(n / 10)

# Comparing the two calculated sums
return sum_n == all_pf_sum

n = int(input());
if (isHoax(n)):
print ("A Hoax Number\n")
else:
print ("Not a Hoax Number\n")
35 changes: 35 additions & 0 deletions Mathematics/juggler_seq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import math

#This function prints the juggler Sequence
# contributed by Nikita Tiwari


def printJuggler(n) :
a = n

# print the first term
print a,

# calculate terms until last term is not 1
while (a != 1) :
b = 0

# Check if previous term is even or odd
if (a%2 == 0) :

# calculate next term
b = (int)(math.floor(math.sqrt(a)))

else :
# for odd previous term calculate
# next term
b = (int) (math.floor(math.sqrt(a)*math.sqrt(a)*
math.sqrt(a)))

print b,
a = b

printJuggler(3)
print
printJuggler(9)
18 changes: 18 additions & 0 deletions Mathematics/modular-exp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>

int power(int x, unsigned int y, int p) {
int res = 1;
x = x % p;

while (y > 0) {
if (y & 1)
res = (res*x) % p;
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
int main() {
printf("%u", power(2, 32, 1024));
return 0;
}
43 changes: 43 additions & 0 deletions Mathematics/politeness_of_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# Find politeness of number
# Contributed by Nikita Tiwari
# A function to count all odd prime factors
# of a given number n


def countOddPrimeFactors(n) :
result = 1;

# Eliminate all even prime factor of
# number of n
while (n % 2 == 0) :
n /= 2

# n must be odd at this point, so iterate
# for only odd numbers till sqrt(n)
i = 3
while i * i <= n :
divCount = 0

# if i divides n, then start counting
# of Odd divisors
while (n % i == 0) :
n /= i
divCount = divCount + 1

result = result * divCount + 1
i = i + 2

# If n odd prime still remains then count it
if (n > 2) :
result = result * 2

return result


def politness( n) :
return countOddPrimeFactors(n) - 1;

n = int(input())
print "Politness of ", n, " = ", politness(n)

0 comments on commit ef36742

Please sign in to comment.