mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-07-11 14:36:12 +08:00
Merge branch 'master' into check_amicable_pair
This commit is contained in:
@@ -1,64 +1,73 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief A simple program to check if the given number is a factorial of some
|
||||
* number or not.
|
||||
* @brief A simple program to check if the given number is a
|
||||
* [factorial](https://en.wikipedia.org/wiki/Factorial) of some number or not.
|
||||
*
|
||||
* @details A factorial number is the sum of k! where any value of k is a
|
||||
* positive integer. https://www.mathsisfun.com/numbers/factorial.html
|
||||
*
|
||||
* @author [Divyajyoti Ukirde](https://github.com/divyajyotiuk)
|
||||
* @author [ewd00010](https://github.com/ewd00010)
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for cout
|
||||
|
||||
/**
|
||||
* Function to check if the given number is factorial of some number or not.
|
||||
* @param n number to be checked.
|
||||
* @return if number is a factorial, returns true, else false.
|
||||
* @namespace
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* @brief Function to check if the given number is factorial of some number or
|
||||
* not.
|
||||
* @param n number to be checked.
|
||||
* @return true if number is a factorial returns true
|
||||
* @return false if number is not a factorial
|
||||
*/
|
||||
|
||||
bool is_factorial(uint64_t n) {
|
||||
if (n <= 0) {
|
||||
if (n <= 0) { // factorial numbers are only ever positive Integers
|
||||
return false;
|
||||
}
|
||||
for (uint32_t i = 1;; i++) {
|
||||
if (n % i != 0) {
|
||||
break;
|
||||
}
|
||||
n = n / i;
|
||||
}
|
||||
if (n == 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Test function
|
||||
/*!
|
||||
* this loop is basically a reverse factorial calculation, where instead
|
||||
* of multiplying we are dividing. We start at i = 2 since i = 1 has
|
||||
* no impact division wise
|
||||
*/
|
||||
int i = 2;
|
||||
while (n % i == 0) {
|
||||
n = n / i;
|
||||
i++;
|
||||
}
|
||||
|
||||
/*!
|
||||
* if n was the sum of a factorial then it should be divided until it
|
||||
* becomes 1
|
||||
*/
|
||||
return (n == 1);
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
void tests() {
|
||||
std::cout << "Test 1:\t n=50\n";
|
||||
assert(is_factorial(50) == false);
|
||||
std::cout << "passed\n";
|
||||
static void tests() {
|
||||
assert(math::is_factorial(50) == false);
|
||||
assert(math::is_factorial(720) == true);
|
||||
assert(math::is_factorial(0) == false);
|
||||
assert(math::is_factorial(1) == true);
|
||||
assert(math::is_factorial(479001600) == true);
|
||||
assert(math::is_factorial(-24) == false);
|
||||
|
||||
std::cout << "Test 2:\t n=720\n";
|
||||
assert(is_factorial(720) == true);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 3:\t n=0\n";
|
||||
assert(is_factorial(0) == false);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 4:\t n=479001600\n";
|
||||
assert(is_factorial(479001600) == true);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 5:\t n=-24\n";
|
||||
assert(is_factorial(-24) == false);
|
||||
std::cout << "passed\n";
|
||||
std::cout << "All tests have successfully passed!" << std::endl;
|
||||
}
|
||||
|
||||
/** Main function
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
tests();
|
||||
tests(); // run self-test implementations
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,62 +1,84 @@
|
||||
/**
|
||||
* Copyright 2020 @author omkarlanghe
|
||||
*
|
||||
* @file
|
||||
* A simple program to check if the given number if prime or not.
|
||||
*
|
||||
* @brief
|
||||
* Reduced all possibilities of a number which cannot be prime.
|
||||
* Eg: No even number, except 2 can be a prime number, hence we will increment
|
||||
* our loop with i+6 jumping and check for i or i+2 to be a factor of the
|
||||
* number; if it's a factor then we will return false otherwise true after the
|
||||
* loop terminates at the terminating condition which is (i*i<=num)
|
||||
* A simple program to check if the given number is
|
||||
* [Prime](https://en.wikipedia.org/wiki/Primality_test) or not.
|
||||
* @details
|
||||
* A prime number is any number that can be divided only by itself and 1. It
|
||||
* must be positive and a whole number, therefore any prime number is part of
|
||||
* the set of natural numbers. The majority of prime numbers are even numbers,
|
||||
* with the exception of 2. This algorithm finds prime numbers using this
|
||||
* information. additional ways to solve the prime check problem:
|
||||
* https://cp-algorithms.com/algebra/primality_tests.html#practice-problems
|
||||
* @author [Omkar Langhe](https://github.com/omkarlanghe)
|
||||
* @author [ewd00010](https://github.com/ewd00010)
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* Function to check if the given number is prime or not.
|
||||
* @param num number to be checked.
|
||||
* @return if number is prime, it returns @ true, else it returns @ false.
|
||||
* @brief Mathematical algorithms
|
||||
* @namespace
|
||||
*/
|
||||
template <typename T>
|
||||
bool is_prime(T num) {
|
||||
bool result = true;
|
||||
namespace math {
|
||||
/**
|
||||
* @brief Function to check if the given number is prime or not.
|
||||
* @param num number to be checked.
|
||||
* @return true if number is a prime
|
||||
* @return false if number is not a prime.
|
||||
*/
|
||||
bool is_prime(int64_t num) {
|
||||
/*!
|
||||
* Reduce all possibilities of a number which cannot be prime with the first
|
||||
* 3 if, else if conditionals. Example: Since no even number, except 2 can
|
||||
* be a prime number and the next prime we find after our checks is 5,
|
||||
* we will start the for loop with i = 5. then for each loop we increment
|
||||
* i by +6 and check if i or i+2 is a factor of the number; if it's a factor
|
||||
* then we will return false. otherwise, true will be returned after the
|
||||
* loop terminates at the terminating condition which is i*i <= num
|
||||
*/
|
||||
if (num <= 1) {
|
||||
return false;
|
||||
} else if (num == 2 || num == 3) {
|
||||
return true;
|
||||
} else if ((num % 2) == 0 || num % 3 == 0) {
|
||||
} else if (num % 2 == 0 || num % 3 == 0) {
|
||||
return false;
|
||||
} else {
|
||||
for (T i = 5; (i * i) <= (num); i = (i + 6)) {
|
||||
if ((num % i) == 0 || (num % (i + 2) == 0)) {
|
||||
result = false;
|
||||
break;
|
||||
for (int64_t i = 5; i * i <= num; i = i + 6) {
|
||||
if (num % i == 0 || num % (i + 2) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
return true;
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void tests() {
|
||||
assert(math::is_prime(1) == false);
|
||||
assert(math::is_prime(2) == true);
|
||||
assert(math::is_prime(3) == true);
|
||||
assert(math::is_prime(4) == false);
|
||||
assert(math::is_prime(-4) == false);
|
||||
assert(math::is_prime(7) == true);
|
||||
assert(math::is_prime(-7) == false);
|
||||
assert(math::is_prime(19) == true);
|
||||
assert(math::is_prime(50) == false);
|
||||
assert(math::is_prime(115249) == true);
|
||||
|
||||
std::cout << "All tests have successfully passed!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
// perform self-test
|
||||
assert(is_prime(50) == false);
|
||||
assert(is_prime(115249) == true);
|
||||
|
||||
int num = 0;
|
||||
std::cout << "Enter the number to check if it is prime or not" << std::endl;
|
||||
std::cin >> num;
|
||||
bool result = is_prime(num);
|
||||
if (result) {
|
||||
std::cout << num << " is a prime number" << std::endl;
|
||||
} else {
|
||||
std::cout << num << " is not a prime number" << std::endl;
|
||||
}
|
||||
|
||||
tests(); // perform self-tests implementations
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -15,10 +15,9 @@
|
||||
* @author [Swastika Gupta](https://github.com/Swastyy)
|
||||
*/
|
||||
|
||||
#include <algorithm> /// for std::is_equal, std::swap
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
@@ -39,10 +38,17 @@ namespace n_bonacci {
|
||||
* @returns the n-bonacci sequence as vector array
|
||||
*/
|
||||
std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
|
||||
std::vector<uint64_t> a(m, 0); // we create an empty array of size m
|
||||
std::vector<uint64_t> a(
|
||||
m, 0); // we create an array of size m filled with zeros
|
||||
if (m < n || n == 0) {
|
||||
return a;
|
||||
}
|
||||
|
||||
a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of
|
||||
/// preceding N zeros
|
||||
if (n == m) {
|
||||
return a;
|
||||
}
|
||||
a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is
|
||||
/// also 1
|
||||
for (uint64_t i = n + 1; i < m; i++) {
|
||||
@@ -61,55 +67,33 @@ std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
// n = 1 m = 1 return [1, 1]
|
||||
std::cout << "1st test";
|
||||
std::vector<uint64_t> arr1 = math::n_bonacci::N_bonacci(
|
||||
1, 1); // first input is the param n and second one is the param m for
|
||||
// N-bonacci func
|
||||
std::vector<uint64_t> output_array1 = {
|
||||
1, 1}; // It is the expected output series of length m
|
||||
assert(std::equal(std::begin(arr1), std::end(arr1),
|
||||
std::begin(output_array1)));
|
||||
std::cout << "passed" << std::endl;
|
||||
struct TestCase {
|
||||
const uint64_t n;
|
||||
const uint64_t m;
|
||||
const std::vector<uint64_t> expected;
|
||||
TestCase(const uint64_t in_n, const uint64_t in_m,
|
||||
std::initializer_list<uint64_t> data)
|
||||
: n(in_n), m(in_m), expected(data) {
|
||||
assert(data.size() == m);
|
||||
}
|
||||
};
|
||||
const std::vector<TestCase> test_cases = {
|
||||
TestCase(0, 0, {}),
|
||||
TestCase(0, 1, {0}),
|
||||
TestCase(0, 2, {0, 0}),
|
||||
TestCase(1, 0, {}),
|
||||
TestCase(1, 1, {1}),
|
||||
TestCase(1, 2, {1, 1}),
|
||||
TestCase(1, 3, {1, 1, 1}),
|
||||
TestCase(5, 15, {0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464}),
|
||||
TestCase(
|
||||
6, 17,
|
||||
{0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976}),
|
||||
TestCase(56, 15, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
|
||||
|
||||
// n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236,
|
||||
// 464]
|
||||
std::cout << "2nd test";
|
||||
std::vector<uint64_t> arr2 = math::n_bonacci::N_bonacci(
|
||||
5, 15); // first input is the param n and second one is the param m for
|
||||
// N-bonacci func
|
||||
std::vector<uint64_t> output_array2 = {
|
||||
0, 0, 0, 0, 1, 1, 2, 4,
|
||||
8, 16, 31, 61, 120, 236, 464}; // It is the expected output series of
|
||||
// length m
|
||||
assert(std::equal(std::begin(arr2), std::end(arr2),
|
||||
std::begin(output_array2)));
|
||||
std::cout << "passed" << std::endl;
|
||||
|
||||
// n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248,
|
||||
// 492, 976]
|
||||
std::cout << "3rd test";
|
||||
std::vector<uint64_t> arr3 = math::n_bonacci::N_bonacci(
|
||||
6, 17); // first input is the param n and second one is the param m for
|
||||
// N-bonacci func
|
||||
std::vector<uint64_t> output_array3 = {
|
||||
0, 0, 0, 0, 0, 1, 1, 2, 4,
|
||||
8, 16, 32, 63, 125, 248, 492, 976}; // It is the expected output series
|
||||
// of length m
|
||||
assert(std::equal(std::begin(arr3), std::end(arr3),
|
||||
std::begin(output_array3)));
|
||||
std::cout << "passed" << std::endl;
|
||||
|
||||
// n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
std::cout << "4th test";
|
||||
std::vector<uint64_t> arr4 = math::n_bonacci::N_bonacci(
|
||||
56, 15); // first input is the param n and second one is the param m
|
||||
// for N-bonacci func
|
||||
std::vector<uint64_t> output_array4 = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0}; // It is the expected output series of length m
|
||||
assert(std::equal(std::begin(arr4), std::end(arr4),
|
||||
std::begin(output_array4)));
|
||||
for (const auto &tc : test_cases) {
|
||||
assert(math::n_bonacci::N_bonacci(tc.n, tc.m) == tc.expected);
|
||||
}
|
||||
std::cout << "passed" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user