Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Recursions
- {
- internal class Program
- {
- static int Factorial(int n)
- {
- if (n == 0)
- {
- return 1;
- }
- return n * Factorial(n - 1);
- }
- static int Fib(int n)
- {
- if (n == 0 || n == 1)
- {
- return 1;
- }
- return Fib(n - 2) + Fib(n - 1);
- }
- static void NToOne(int n)
- {
- if (n == 0)
- {
- return;
- }
- Console.WriteLine(n);
- NToOne(n - 1);
- }
- static void OneToN(int n)
- {
- if (n == 0)
- {
- return;
- }
- OneToN(n - 1);
- Console.WriteLine(n);
- }
- // O(n)
- static double Power(double a, int n)
- {
- if (n == 0)
- {
- return 1;
- }
- if (n < 0)
- {
- return 1 / Power(a, -n);
- }
- return a * Power(a, n - 1);
- }
- // O(log(n))
- static double FastPower(double a, int n)
- {
- if (n == 0)
- {
- return 1;
- }
- if (n < 0)
- {
- return 1 / FastPower(a, -n);
- }
- if (n % 2 == 0)
- {
- double x = FastPower(a, n / 2);
- return x * x;
- }
- double y = FastPower(a, (n - 1) / 2);
- return a * y * y;
- }
- static int GCD(int a, int b)
- {
- if (b == 0)
- {
- return a;
- }
- return GCD(b, a % b);
- }
- static void Main(string[] args)
- {
- Console.WriteLine(GCD(250, 100));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement