rename Math -> math (#651)

This commit is contained in:
Christian Clauss
2019-11-28 13:31:18 +01:00
committed by GitHub
parent 2c09d15334
commit 3fc7de16c0
4 changed files with 0 additions and 0 deletions

View File

@@ -1,91 +0,0 @@
#include <iostream>
using namespace std;
// Maximum number of digits in output
// x^n where 1 <= x, n <= 10000 and overflow may happen
#define MAX 100000
// This function multiplies x
// with the number represented by res[].
// res_size is size of res[] or
// number of digits in the number
// represented by res[]. This function
// uses simple school mathematics
// for multiplication.
// This function may value of res_size
// and returns the new value of res_size
int multiply(int x, int res[], int res_size)
{
// Initialize carry
int carry = 0;
// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < res_size; i++)
{
int prod = res[i] * x + carry;
// Store last digit of
// 'prod' in res[]
res[i] = prod % 10;
// Put rest in carry
carry = prod / 10;
}
// Put carry in res and
// increase result size
while (carry)
{
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
// This function finds
// power of a number x
void power(int x, int n)
{
//printing value "1" for power = 0
if (n == 0)
{
cout << "1";
return;
}
int res[MAX];
int res_size = 0;
int temp = x;
// Initialize result
while (temp != 0)
{
res[res_size++] = temp % 10;
temp = temp / 10;
}
// Multiply x n times
// (x^n = x*x*x....n times)
for (int i = 2; i <= n; i++)
res_size = multiply(x, res, res_size);
cout << x << "^" << n << " = ";
for (int i = res_size - 1; i >= 0; i--)
cout << res[i];
}
// Driver program
int main()
{
int exponent, base;
printf("Enter base ");
scanf("%id \n", &base);
printf("Enter exponent ");
scanf("%id", &exponent);
power(base, exponent);
return 0;
}

View File

@@ -1,12 +0,0 @@
Prime Factorization is a very important and useful technique to factorize any number into its prime factors. It has various applications in the field of number theory.
The method of prime factorization involves two function calls.
First: Calculating all the prime number up till a certain range using the standard
Sieve of Eratosthenes.
Second: Using the prime numbers to reduce the the given number and thus find all its prime factors.
The complexity of the solution involves approx. O(n logn) in calculating sieve of eratosthenes
O(log n) in calculating the prime factors of the number. So in total approx. O(n logn).
**Requirements: For compile you need the compiler flag for C++ 11**

View File

@@ -1,81 +0,0 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Declaring variables for maintaing prime numbers and to check whether a number is prime or not
bool isprime[1000006];
vector<int> prime_numbers;
vector<pair<int, int>> factors;
// Calculating prime number upto a given range
void SieveOfEratosthenes(int N)
{
// initializes the array isprime
memset(isprime, true, sizeof isprime);
for (int i = 2; i <= N; i++)
{
if (isprime[i])
{
for (int j = 2 * i; j <= N; j += i)
isprime[j] = false;
}
}
for (int i = 2; i <= N; i++)
{
if (isprime[i])
prime_numbers.push_back(i);
}
}
// Prime factorization of a number
void prime_factorization(int num)
{
int number = num;
for (int i = 0; prime_numbers[i] <= num; i++)
{
int count = 0;
// termination condition
if (number == 1)
{
break;
}
while (number % prime_numbers[i] == 0)
{
count++;
number = number / prime_numbers[i];
}
if (count)
factors.push_back(make_pair(prime_numbers[i], count));
}
}
/*
I added a simple UI.
*/
int main()
{
int num;
cout << "\t\tComputes the prime factorization\n\n";
cout << "Type in a number: ";
cin >> num;
SieveOfEratosthenes(num);
prime_factorization(num);
// Prime factors with their powers in the given number in new line
for (auto it : factors)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

View File

@@ -1,69 +0,0 @@
/*
* Sieve of Eratosthenes is an algorithm to find the primes
* that is between 2 to N (as defined in main).
*
* Time Complexity : O(N * log N)
* Space Complexity : O(N)
*/
#include <iostream>
using namespace std;
#define MAX 10000000
int isprime[MAX];
/*
* This is the function that finds the primes and eliminates
* the multiples.
*/
void sieve(int N)
{
isprime[0] = 0;
isprime[1] = 0;
for (int i = 2; i <= N; i++)
{
if (isprime[i])
{
for (int j = i * 2; j <= N; j += i)
{
isprime[j] = 0;
}
}
}
}
/*
* This function prints out the primes to STDOUT
*/
void print(int N)
{
for (int i = 1; i <= N; i++)
{
if (isprime[i] == 1)
{
cout << i << ' ';
}
}
cout << '\n';
}
/*
* NOTE: This function is important for the
* initialization of the array.
*/
void init()
{
for (int i = 1; i < MAX; i++)
{
isprime[i] = 1;
}
}
int main()
{
int N = 100;
init();
sieve(N);
print(N);
}