Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- using namespace std;
- #pragma region NWD
- // Algorytm Euklidesa
- unsigned int NWD_Euk(unsigned int a, unsigned int b) {
- while (a != b) {
- if (a > b)
- a -= b;
- else
- b -= a;
- }
- return a;
- }
- // dzielenie z resztą
- // https://www.naukowiec.org/wiedza/matematyka/nwd--najwiekszy-wspolny-dzielnik_1183.html
- int NWD_Reszt(unsigned int a, unsigned int b) {
- while (true) {
- if (a > b)
- a = a % b;
- else
- b = b % a;
- if (a == 0 || b == 0)
- // zwracamy większą
- return max(a, b);
- }
- }
- #pragma endregion
- #pragma region Silnia
- // rekurencyjnie
- int Silnia_Rek(unsigned int n) {
- if (n < 2)
- return 1;
- return n * Silnia_Rek(n - 1);
- }
- // tablica (szybszy)
- vector<int> Silnie_Tab = { 1,1 };
- int Silnia_Tab(unsigned int n) {
- int a = n - Silnie_Tab.size();
- // dopełniamy tablicę z silniami aż do n
- for (int i = 0; i <= a; i++) {
- Silnie_Tab.push_back(Silnie_Tab.size() * Silnie_Tab[Silnie_Tab.size() - 1]);
- }
- return Silnie_Tab[n];
- }
- #pragma endregion
- #pragma region Fibonacci
- // rekurencyjnie
- int Fib_Rek(unsigned int n) {
- if (n < 2)
- return 1;
- return Fib_Rek(n - 1) + Fib_Rek(n - 2);
- }
- // tablica (szybszy)
- vector<int> Fiby_Tab = { 1,1 };
- int Fib_Tab(unsigned int n) {
- int a = n - Fiby_Tab.size();
- // dopełniamy tablicę ciągiem Fibonacciego az do n
- for (int i = 0; i <= a; i++)
- {
- Fiby_Tab.push_back(Fiby_Tab[Fiby_Tab.size() - 1] + Fiby_Tab[Fiby_Tab.size() - 2]);
- }
- return Fiby_Tab[n];
- }
- #pragma endregion
- #pragma region NWW
- // NWW z NWD
- int NWW(unsigned int a, unsigned int b) {
- return (a * b) / NWD_Euk(a, b);
- }
- // NWW Z petla
- int NWW_Petla(unsigned int a, unsigned int b) {
- // upewniamy sie ze w a jest wieksza liczba
- if (a < b)
- swap(a, b);
- int i = 2;
- // sprawdzamy kazda kolejna wielokrotnosc liczby a
- while(true){
- int w = a * i;
- // az jest podzielna przez b, wtedy to NWW
- if (w % b == 0)
- return w;
- i++;
- }
- }
- #pragma endregion
- #pragma region SumaCyfr
- int SumaCyfr_Reszta(int liczba) {
- int suma = 0;
- while (liczba != 0) {
- // reszta z dzielenie przez 10 to ostatnia cyfra liczby
- suma += liczba % 10;
- liczba /= 10;
- }
- return suma;
- }
- int SumaCyfr_String(int liczba) {
- int suma = 0;
- // to_string z biblioteki string - abs to wartośc bezwzględna
- string liczba_text = to_string(abs(liczba));
- for (char c : liczba_text) {
- suma += c - '0';
- }
- return suma;
- }
- #pragma endregion
- #pragma region SystemyNumeryczne
- int ToDecimal(std::string number, unsigned int numeric_system) {
- int ret = 0;
- for (int i = 0; i < number.length(); i++) {
- int digit;
- // check if char is a letter
- if (number[i] >= 'A' && number[i] <= 'Z')
- // yep, so convert it into 2 digit number
- digit = number[i] - 'A' + 10;
- else
- // nope, get single digit
- digit = number[i] - '0';
- // add represented number to return number
- ret += pow(numeric_system, number.size() - 1 - i) * digit;
- }
- return ret;
- }
- string ChangeNumericSystem(unsigned int decimal, unsigned int numeric_system) {
- string ret;
- // reapeating till whole number is converted
- while (decimal != 0) {
- // get the rest of the division (modulo operator)
- int rest = decimal % numeric_system;
- // check if we must write it as a letter
- if (rest > 9)
- // yep, change it into letter and then add to return string
- ret += (char)('A' + rest - 10);
- else
- // nope, just add it to return string
- ret += to_string(rest);
- decimal /= numeric_system;
- }
- // reverse the string, to get proper number
- for (int i = 0; i < ret.length() / 2; i++) {
- swap(ret[i], ret[ret.length() - 1 - i]);
- }
- return ret;
- }
- #pragma endregion
- #pragma region Inne
- bool CzyPierwsza(int liczba) {
- if (liczba <=2)
- return liczba==2;
- if (liczba % 2 == 0)
- return false;
- for (int i = 3; i < liczba / 2; i+=2) {
- if (liczba % i == 0)
- return false;
- }
- }
- #pragma endregion
- int main()
- {
- // Testy
- // NWD
- cout << endl << "NWD" << endl;
- cout << NWD_Euk(10, 20) << " "<< NWD_Reszt(10,20) << endl;
- cout << NWD_Euk(117649, 2401) << " " << NWD_Reszt(117649, 2401) << endl;
- cout << NWD_Euk(441, 25) << " " << NWD_Reszt(441, 25) << endl;
- cout << NWD_Euk(58935, 4242) << " " << NWD_Reszt(58935, 4242) << endl;
- //Silnia
- cout << endl << "Silnia" << endl;
- for (int i = 0; i < 14; i++) {
- cout << Silnia_Rek(i) << " " << Silnia_Tab(i) << endl;
- }
- //Fibonacci
- cout << endl << "Liczby Fibonacciego" << endl;
- for (int i = 0; i < 30; i++) {
- cout << Fib_Rek(i) << " " << Fib_Tab(i) << endl;
- }
- //NWW
- cout << endl << "NWW" << endl;
- cout << NWW(5, 11) << " " << NWW_Petla(5,11) << endl;
- cout << NWW(304, 23) << " " << NWW_Petla(304, 23) << endl;
- cout << NWW(3, 17) << " " << NWW_Petla(3, 17) << endl;
- cout << NWW(9, 83) << " " << NWW_Petla(9, 83) << endl;
- //Suma Cyfr
- cout << endl<<"Suma Cyfr" << endl;
- cout << SumaCyfr_Reszta(245) << " " << SumaCyfr_String(245) << endl;
- cout << SumaCyfr_Reszta(309) << " " << SumaCyfr_String(309) << endl;
- cout << SumaCyfr_Reszta(13124141) << " " << SumaCyfr_String(13124141) << endl;
- cout << SumaCyfr_Reszta(0) << " " << SumaCyfr_String(0) << endl;
- cout << SumaCyfr_Reszta(312412) << " " << SumaCyfr_String(312412) << endl;
- //Liczby Pierwsze
- cout << endl << "Czy Liczba Pierwsza" << endl;
- for (int i = 70; i <= 150; i++) {
- cout << i << " " << (CzyPierwsza(i) ? "tak" : "nie") << endl;
- }
- // Systemy
- cout << endl << "Systemy Liczbowe" << endl;
- for (int i = 999; i < 2000; i+=9) {
- cout << i << " (2," << ChangeNumericSystem(i, 2) << ", " << ToDecimal(ChangeNumericSystem(i, 2), 2) << ")" << " (8," << ChangeNumericSystem(i, 8) << ", " << ToDecimal(ChangeNumericSystem(i, 8), 8) << ")" << " (16," << ChangeNumericSystem(i, 16) << ", " << ToDecimal(ChangeNumericSystem(i, 16), 16) << ")" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment