Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <vector>
- using ulong = unsigned long long;
- using vector = std::vector<std::pair<ulong, int>>;
- std::string wypisz_czynniki(const vector& czynniki) {
- auto d{ czynniki.size() - 1};
- std::stringstream result{};
- auto out{ [czynniki, &result](const auto x) {
- result << czynniki[x].first;
- if (czynniki[x].second > 1) {
- result << '^' << czynniki[x].second;
- }
- } };
- for (int i{}; i < d; ++i) {
- out(i);
- result << " * ";
- }
- out(d);
- return result.str();
- }
- ulong pobierz_liczbe() {
- std::cout << "Podaj liczbe naturalna lub 0 jesli chcesz wyjsc: ";
- ulong result;
- std::cin >> result;
- while (std::cin.fail()) {
- std::cout << "\tPodales nieprawidlowa liczbe, wpisz jeszcze raz: ";
- std::cin.clear();
- std::cin.ignore(999999, '\n');
- std::cin >> result;
- }
- return result;
- }
- bool rozloz_liczbe() {
- auto liczba{ pobierz_liczbe() };
- if (liczba == 0) {
- return false;
- }
- if (liczba == 1) {
- std::cout << "\tLiczby 1 nie da sie rozlozyc na czynniki pierwsze\n";
- return true;
- }
- const auto rozkladana{ liczba };
- ulong dzielnik{ 2 };
- vector czynniki{};
- while (liczba > 1) {
- if (liczba % dzielnik == 0) {
- auto p{ std::make_pair(dzielnik, 0) };
- do {
- ++(p.second);
- liczba /= dzielnik;
- } while (liczba % dzielnik == 0);
- czynniki.push_back(p);
- }
- ++dzielnik;
- }
- std::cout << '\t' << rozkladana << " = " << wypisz_czynniki(czynniki) << "\n\n";
- return true;
- }
- int main() {
- while (rozloz_liczbe()) {};
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment