This repository has been archived on 2023-05-13. You can view files and clone it, but cannot push or open issues or pull requests.
mipt_cpp/seminar02_encapsulation/1number/main.cpp

76 lines
1.5 KiB
C++
Raw Normal View History

2022-09-14 19:05:27 +03:00
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
2022-09-15 08:40:48 +03:00
#include "number.h"
2022-09-14 19:05:27 +03:00
2022-09-15 10:29:14 +03:00
Number fib(int n)
{
Number a = 0; // F0
Number b = 1; // F1
for (int i = 1; i <= n; ++i) {
if (i % 2) {
a += b;
} else {
b += a;
}
2022-09-14 19:05:27 +03:00
}
if (n % 2) {
2022-09-15 10:29:14 +03:00
return b;
2022-09-14 19:05:27 +03:00
}
return a;
}
2022-09-15 10:29:14 +03:00
Number factorial(int n)
{
Number result {
1};
2022-09-14 19:05:27 +03:00
for (int i = 2; i < n + 1; ++i) {
2022-09-15 10:29:14 +03:00
result = Number(i) * result;
2022-09-14 19:05:27 +03:00
}
return result;
}
2022-09-15 10:29:14 +03:00
void grad(Number n)
{
2022-09-15 08:21:25 +03:00
std::cout << "n = " << n;
Number max = n;
unsigned long long int steps = 0;
while (n != Number(1)) {
2022-09-15 10:29:14 +03:00
if (n > max) {
2022-09-15 08:21:25 +03:00
#ifdef _DEBUG_COMP
2022-09-15 10:29:14 +03:00
std::cout << n << " is greater than " << max << std::endl;
2022-09-15 08:21:25 +03:00
#endif
2022-09-15 10:29:14 +03:00
max = n;
}
if (n.isEven()) {
n.div2();
} else {
n = Number(3) * n + Number(1);
}
2022-09-15 08:40:48 +03:00
#ifdef _DEBUG_GRAD
2022-09-15 10:29:14 +03:00
if (steps > 100) {
std::cout << "break" << std::endl;
break;
}
2022-09-15 08:40:48 +03:00
#endif
2022-09-15 10:29:14 +03:00
++steps;
2022-09-15 08:21:25 +03:00
}
2022-09-15 10:29:14 +03:00
std::cout << " steps = " << steps << " max = " << max << std::endl;
2022-09-15 08:21:25 +03:00
}
2022-09-15 10:29:14 +03:00
int main()
2022-09-14 19:05:27 +03:00
{
2022-09-15 10:29:14 +03:00
std::cout << "===FIB===" << std::endl;
std::cout << "F(1000) = " << fib(1000) << std::endl;
std::cout << "===FAC===" << std::endl;
std::cout << "1000! = " << factorial(1000) << std::endl;
std::cout << "===GRAD===" << std::endl;
grad(Number("7"));
grad(Number("256"));
grad(Number("1117065"));
grad(Number("4761963248413673697"));
grad(Number("90560792656972947582439785608972465789628974587264056284658721771"));
2022-09-14 19:05:27 +03:00
}