diff options
16 files changed, 277 insertions, 0 deletions
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task3 - isPositive().cpp (renamed from pract_SI/Pract. 04/Solutions/task3.cpp) | 0 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task4 - abs.cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task4 - fabs.cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task6.cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task7 - a).cpp | 3 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task7 - b).cpp | 3 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task8.cpp | 9 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task9.cpp | 17 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task10.cpp | 9 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task11.cpp | 25 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp | 12 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task12.cpp | 35 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task13.cpp | 43 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task14.cpp | 92 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Tasks4.txt | 13 |
diff --git a/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp b/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp new file mode 100644 index 0000000..6ca43c5 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp @@ -0,0 +1,4 @@ +bool isNegative(int num) +{ + return num < 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task3.cpp b/pract_SI/Pract. 04/Solutions/task3 - isPositive().cpp index 937b833..937b833 100644 --- a/pract_SI/Pract. 04/Solutions/task3.cpp +++ b/pract_SI/Pract. 04/Solutions/task3 - isPositive().cpp diff --git a/pract_SI/Pract. 04/Solutions/task4 - abs.cpp b/pract_SI/Pract. 04/Solutions/task4 - abs.cpp new file mode 100644 index 0000000..771550b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task4 - abs.cpp @@ -0,0 +1,4 @@ +int abs(int num1, int num2) +{ + return ((num1 - num2) >= 0 ? (num1 - num2) : (num2 - num1)); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp b/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp new file mode 100644 index 0000000..bb2f15b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp @@ -0,0 +1,4 @@ +double fabs(double &num) +{ + return num > 0.0 ? num : -num; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task6.cpp b/pract_SI/Pract. 04/Solutions/task6.cpp new file mode 100644 index 0000000..cbca3de --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task6.cpp @@ -0,0 +1,4 @@ +bool isCapitalLetter(char S) +{ + return (S >= 'A' && S <= 'Z'); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task7 - a).cpp b/pract_SI/Pract. 04/Solutions/task7 - a).cpp new file mode 100644 index 0000000..8a590b4 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task7 - a).cpp @@ -0,0 +1,3 @@ +bool isInInterval(int num) { + return num >= 100 && num <= 10000; +} diff --git a/pract_SI/Pract. 04/Solutions/task7 - b).cpp b/pract_SI/Pract. 04/Solutions/task7 - b).cpp new file mode 100644 index 0000000..4dde747 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task7 - b).cpp @@ -0,0 +1,3 @@ +bool isNumberInInterval(long longnum, long long interval_lower_bound, long long interval_upper_bound,) { + return num >= interval_lower_bound && num <= interval_upper_bound; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task8.cpp b/pract_SI/Pract. 04/Solutions/task8.cpp new file mode 100644 index 0000000..9d91c6a --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task8.cpp @@ -0,0 +1,9 @@ +long long pow(int num, unsigned N) +{ + long long result = 1; + + for (size_t i = 0; i < N; i++) + result *= num; + + return result; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task9.cpp b/pract_SI/Pract. 04/Solutions/task9.cpp new file mode 100644 index 0000000..0356de8 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task9.cpp @@ -0,0 +1,17 @@ +int calculate(int a, int b, char action = '+') +{ + switch (action) + { + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + case '%': return a % b; + + default: return -1; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task10.cpp b/pract_SI/Pract. 04/Solutions_10-14/task10.cpp new file mode 100644 index 0000000..e9e83b9 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task10.cpp @@ -0,0 +1,9 @@ +int askUser(int from, int to) +{ + int number; + while (std::cin >> number) + { + if (number >= from && number <= to) + return number; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task11.cpp b/pract_SI/Pract. 04/Solutions_10-14/task11.cpp new file mode 100644 index 0000000..77d76e5 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task11.cpp @@ -0,0 +1,25 @@ +void convertIfNegative(int &number) +{ + if (number < 0) + number *= -1; +} + +unsigned long long toBinary(int number) +{ + convertIfNegative(number); + + unsigned long long toBinary = 0; + short digits = 0; + while (number > 0) + { + unsigned long long temp = 1; + for (size_t i = 0; i < digits; i++) + temp *= 10; + + toBinary += (number % 2) * temp; + digits++; + number /= 2; + } + + return toBinary; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp b/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp new file mode 100644 index 0000000..1b9a3e3 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp @@ -0,0 +1,12 @@ +/* +NOTE! +Current Solution uses recursion, which we still have NOT taken/discussed. +*/ + +void toBinary(unsigned int num) +{ + if (num > 1) { + toBinary(num / 2); + } + std::cout << num % 2; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task12.cpp b/pract_SI/Pract. 04/Solutions_10-14/task12.cpp new file mode 100644 index 0000000..464b17b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task12.cpp @@ -0,0 +1,35 @@ +#include <iostream> +using namespace std; + +int gcd(int x, int y) +{ + while (x != y) + if (x > y) + x -= y; + else + y -= x; + return x; +} + +int main() +{ + int a, b, c, d; + + cout << "a, b, c, d= "; + cin >> a >> b >> c >> d; + if (!cin || a < 1 || b < 1 || c < 1 || d < 1) + { + cout << "Error! \n"; + return 1; + } + + int r = gcd(a, b); + cout << "gcd{" << a << ", " << b << "}= " << r << "\n"; + + int s = gcd(c, d); + cout << "gcd{" << c << ", " << d << "}= " << s << "\n"; + + cout << "gcd{" << a << ", " << b << ", " << c << ", " << d << "}= " << gcd(r, s) << "\n"; + + return 0; +} diff --git a/pract_SI/Pract. 04/Solutions_10-14/task13.cpp b/pract_SI/Pract. 04/Solutions_10-14/task13.cpp new file mode 100644 index 0000000..8acb1c9 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task13.cpp @@ -0,0 +1,43 @@ +#include<iostream> +using namespace std; + +int input() +{ + int number; + do + { + cout << "Enter a valid number: "; + cin >> number; + } while (!cin || number < 100 || number > 100000); + return number; +} + +int dist(int number1, int number2) +{ + int diff = number1 - number2; + return diff >= 0 ? diff : -diff; +} + +int closestLargerPowerOf2(int number) +{ + int power = 1; + while (power < number) + { + power <<= 1; + } + return power; +} + +int main() +{ + int number = input(); + int larger = closestLargerPowerOf2(number); + int smaller = larger >> 1; + + int distToSmaller = dist(number, smaller); + int distToLarger = dist(number, larger); + + cout << "Closest power of two to " << number << ": " << ((distToSmaller < distToLarger) ? smaller : larger) << endl; + + return 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task14.cpp b/pract_SI/Pract. 04/Solutions_10-14/task14.cpp new file mode 100644 index 0000000..405bcf7 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task14.cpp @@ -0,0 +1,92 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +//Function to check whether a number is prime +bool isPrime(int number) +{ + int size = sqrt(number); + + for (int i = 2; i <= size; i++) + { + if (number % i == 0) + return false; + } + + return true; +} + +//Print all prime numbers that are smaller than N +void printAllPrimeToNumber(int number) //Interval [1 ; N] +{ + std::cout << "1" << ", 2"; + + for (int i = 3; i <= number; i += 2) + { + if (isPrime(i)) + std::cout << ", " << i; + } + + std::cout << "." << std::endl; +} + +//PrimeFactorization +void PrimeFactor(int number) +{ + for (int i = 2; i <= number; i++) + { + if (isPrime(i) && number % i == 0) + { + if (i == number) //last step, no space + std::cout << i; + else + std::cout << i << " "; + + number /= i; + i--; + } + } + std::cout << std::endl; +} + +//Function whether the number N can be expressed as a sum of two prime numbers +bool checkSumOfTwoPrimes(int number,int &primeOfNumber) +{ + int size = number / 2; //36 = 7 + 29, 16 = 3 + 13 , 36 = 5 + 31 + for (int i = 2;i <= size; i++) + { + //16 = 3 + x , x = 16 - 3 , x = 13 PRIME + if (isPrime(i) && isPrime(number - i)) + { + primeOfNumber = i; + return true; + } + } + return false; +} + +int main() +{ + int number; + std::cin >> number; + + if (isPrime(number)) + std::cout << number << " is prime!" << std::endl; + else + std::cout << number << " is composite number!" << std::endl; + + std::cout << std::endl << "Prime numbers up to " << number << " are: "; + printAllPrimeToNumber(number); + + std::cout << "Factoring of: " << number << " is: "; + PrimeFactor(number); + + int primeOfNumber; + if (checkSumOfTwoPrimes(number, primeOfNumber)) + std::cout << primeOfNumber << " + " << number - primeOfNumber << std::endl; + else + std::cout << "Error!" << std::endl; + + return 0; +} diff --git a/pract_SI/Pract. 04/Tasks4.txt b/pract_SI/Pract. 04/Tasks4.txt index 4daf06c..5470b9a 100644 --- a/pract_SI/Pract. 04/Tasks4.txt +++ b/pract_SI/Pract. 04/Tasks4.txt @@ -29,4 +29,17 @@ Входа да се валидира като при грешен ход да се дава възможност за нов опит. Изкарайте отделните функционалности във функции. +14. a)Напишете функция, която проверява дали дадено число е просто. +14. b)Напишете функция, която извежда на екрана всички прости числа до дадено число. +14. c)Напишете програма, която приема число и отпечатва неговата *факторизация. +*Декомпозиция на числото на произведението на други прости числа(множители), които след това умножение дават оригинала* + +Вход: 225 +Изход: 3.3.5.5 + +Вход: 31668 +Изход: 2.2.3.7.13.29 + +14. d)Напишете функция, която проверява дали дадено число може да бъде представено като сбор на 2 прости числа. + |
