Для последовательности а1 = 1, an+1 = n⋅ an – 1/n составить программу печати n-го элемента в виде обыкновенной
несократимой дроби
Верстка, CSS, HTML, SVG
Помогите пожалуйста с программированием. Можете написать на С++, С# и Phyton. Желательно C#.
Растет это дело почти так же быстро, как факториал, поэтому при n=10 наступает переполнение. Если очень хочеццо, можешь поиграться в типом BigInteger, хоть он и медленный. А пока - примерно так:
public class Fraction
{
private int Numerator;
private uint Denominator;
private bool Simplified = false;
private void Simplify()
{
uint gcd;
do
{
gcd = GCD((uint)(Numerator*Math.Sign(Numerator)), Denominator);
Numerator /= (int)gcd;
Denominator /= gcd;
}
while (gcd != 1);
Simplified = true;
}
private uint GCD(uint a, uint b)
{
while (a != 0 && b != 0)
if (a > b)
a %= b;
else
b %= a;
return a + b;
}
public override string ToString()
{
if (!Simplified)
Simplify();
return $"{Numerator}/{Denominator}";
}
public Fraction(int _Numerator, uint _Denominator)
{
if (_Denominator == 0)
throw new Exception("Denominator cannot be zero");
Numerator = _Numerator;
Denominator = _Denominator;
}
public static Fraction operator +(Fraction a, Fraction b) => new Fraction(a.Numerator * (int)b.Denominator + b.Numerator * (int)a.Denominator, a.Denominator * b.Denominator);
public static Fraction operator *(int a, Fraction b) => new Fraction(a * b.Numerator, b.Denominator);
public static Fraction operator *(Fraction a, Fraction b) => new Fraction(a.Numerator * b.Numerator, a.Denominator * b.Denominator);
}
internal class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
Fraction f = new Fraction(1, 1);
for (uint i = 1; i < n; i++)
f = (int)i * f + new Fraction(-1, i);
Console.WriteLine(f);
Console.ReadLine();
}
}
Александр Белякин
Спасибо.
На C++ надо уметь обращаться с целыми числами любой длины, например, при помощи модуля gmp:
#include <gmpxx.h>
#include <iostream>
using namespace std;
struct Q { mpz_class chis, znam; };
Q a(int n) { if (n == 1) return {1, 1}; Q q;
int m = n - 1; q = a(m); q.chis = m * m * q.chis - q.znam; q.znam *= m; return q; }
mpz_class gcd(mpz_class x, mpz_class y)
{ mpz_class z; if (x < 0) x = - x; for (;;)
{ z = x % y; if (z == 0) return y; x = y; y= z; } }
int main() { int i, n; mpz_class x; Q q;
for (;;) {cout << "n: "; cin >> n; q = a(n);
x = gcd(q.chis, q.znam); cout << q.chis / x
<< " / " << q.znam / x << endl; } }
А на Пайтоне всё вообще в три строчки:
#include <gmpxx.h>
#include <iostream>
using namespace std;
struct Q { mpz_class chis, znam; };
Q a(int n) { if (n == 1) return {1, 1}; Q q;
int m = n - 1; q = a(m); q.chis = m * m * q.chis - q.znam; q.znam *= m; return q; }
mpz_class gcd(mpz_class x, mpz_class y)
{ mpz_class z; if (x < 0) x = - x; for (;;)
{ z = x % y; if (z == 0) return y; x = y; y= z; } }
int main() { int i, n; mpz_class x; Q q;
for (;;) {cout << "n: "; cin >> n; q = a(n);
x = gcd(q.chis, q.znam); cout << q.chis / x
<< " / " << q.znam / x << endl; } }
А на Пайтоне всё вообще в три строчки:

Александр Белякин
Спасибо.
Похожие вопросы
- Помогите в Веб программирование
- Помогите пожалуйста разобраться с сайтом!!!
- Помогите, пожалуйста, решить данную проблему
- ребят, помогите пожалуйста. сделал сайт, но он не отображается ни в яндексе ни в гугле.
- Помогите пожалуйста! Я начинающий веб-программист, в данный момент почти выучил CSS и HTML,у меня вопрос
- Помогите пожалуйста, нужно сверстать макет html сайта по образцу.
- Здравствуйте, помогите, пожалуйста с html
- JS или Phyton с чего начать путь веб разработчика?
- Изучение веб-программирования (внутри)
- Помогите с программированием, пожалуйста.