Advertisement
vencinachev

Recursions

Sep 3rd, 2021
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Recursion
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static int Fact(int n)
  11.         {
  12.             if (n == 0)
  13.             {
  14.                 return 1;
  15.             }
  16.             return Fact(n - 1) * n;
  17.         }
  18.  
  19.         static void PrintNumbers(int n)
  20.         {
  21.             if (n == 0)
  22.             {
  23.                 return;
  24.             }
  25.             Console.Write($"{n} ");
  26.             PrintNumbers(n - 1);
  27.             Console.Write($"{n} ");
  28.         }
  29.  
  30.         static void DrawTriangle(int n)
  31.         {
  32.             if (n == 0)
  33.             {
  34.                 return;
  35.             }
  36.             Console.WriteLine(new string('*', n));
  37.             DrawTriangle(n - 1);
  38.             Console.WriteLine(new string('*', n));
  39.         }
  40.  
  41.         static double Power(double a, int n)
  42.         {
  43.             if (n == 0)
  44.             {
  45.                 return 1;
  46.             }
  47.             return a * Power(a, n - 1);
  48.         }
  49.  
  50.         static double FastPower(double a, int n)
  51.         {
  52.             if (n == 0)
  53.             {
  54.                 return 1;
  55.             }
  56.             else if (n % 2 == 0)
  57.             {
  58.                 double x = FastPower(a, n / 2);
  59.                 return x * x;
  60.             }
  61.             else
  62.             {
  63.                 return a * FastPower(a, n - 1);
  64.             }
  65.         }
  66.         static void Main(string[] args)
  67.         {
  68.             Console.WriteLine(FastPower(1, 100000));
  69.         }
  70.     }
  71. }
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement