Advertisement
vencinachev

Recursions

Sep 21st, 2023 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Recursions
  4. {
  5.     class Program
  6.     {
  7.         static uint Factorial(uint n)
  8.         {
  9.             if (n == 0)
  10.             {
  11.                 return 1;
  12.             }
  13.             return n * Factorial(n - 1);
  14.         }
  15.  
  16.         static void PrintToN(int n)
  17.         {
  18.             if (n == -1)
  19.             {
  20.                 return;
  21.             }
  22.             PrintToN(n - 1);
  23.             Console.WriteLine(n);
  24.         }
  25.         static void Triangle(int n)
  26.         {
  27.             if (n == 0)
  28.             {
  29.                 return;
  30.             }
  31.             Console.WriteLine(new string('*', n));
  32.             Triangle(n - 1);
  33.             Console.WriteLine(new string('*', n));
  34.  
  35.         }
  36.  
  37.  
  38.         static double Power(double a, int n)
  39.         {
  40.             if (n == 0)
  41.             {
  42.                 return 1;
  43.             }
  44.             if (n < 0)
  45.             {
  46.                 return 1 / Power(a, -n);
  47.             }
  48.             return a * Power(a, n - 1);
  49.         }
  50.  
  51.         static double FastExpo(double a, int n)
  52.         {
  53.             if (n == 0)
  54.             {
  55.                 return 1;
  56.             }
  57.             if (n < 0)
  58.             {
  59.                 return 1 / Power(a, -n);
  60.             }
  61.             if (n % 2 == 0)
  62.             {
  63.                 double res = FastExpo(a, n / 2);
  64.                 return res * res;
  65.             }
  66.             double res = FastExpo(a, (n - 1) / 2);
  67.             return a * res * res;
  68.         }
  69.  
  70.         static void Hanoi(int count, char start, char mid, char end)
  71.         {
  72.             if (count == 1)
  73.             {
  74.                 Console.WriteLine($"{start}->{end}");
  75.                 return;
  76.             }
  77.             Hanoi(count - 1, start, end, mid);
  78.             Hanoi(1, start, mid, end);
  79.             Hanoi(count - 1, mid, start, end);
  80.         }
  81.  
  82.         static void Main(string[] args)
  83.         {
  84.             Hanoi(4, 'A', 'B', 'C');
  85.             //Triangle(10);
  86.             //Console.WriteLine(FastExpo(2, 40));
  87.             /*uint num = uint.Parse(Console.ReadLine());
  88.             uint result = Factorial(num);
  89.             Console.WriteLine(result);*/
  90.         }
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement