Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. /*In the "Tribonacci" sequence, every number is formed
  2.  by the sum of the previous 3 numbers.
  3.  Write a program that prints num numbers from the Tribonacci
  4.  sequence, each on a new line, starting from 1.
  5.  The input comes as a parameter named num.
  6.  The value num will always be positive integer.
  7. ____________________
  8. Input         Output
  9. 4             1 1 2 4
  10. ____________________
  11. 8             1 1 2 4 7 13 24 44
  12. ___________________
  13. */
  14. using System;
  15.  
  16. namespace _0_4TribonacciSequence
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             int num = int.Parse(Console.ReadLine());
  23.             PrintTrib(num);
  24.         }
  25.  
  26.         private static int GetTribonacci(int num)
  27.         {
  28.             if (num <= 2)
  29.             {
  30.                 return 1;
  31.             }
  32.            
  33.             if (num == 3)
  34.             {
  35.                 return 2;
  36.             }
  37.             else
  38.             {
  39.                 return GetTribonacci(num - 3) +
  40.                 GetTribonacci(num - 2) +
  41.                     GetTribonacci(num - 1);
  42.             }
  43.         }
  44.  
  45.         private static void PrintTrib(int num)
  46.         {
  47.             for (int i = 1; i <= num; i++)
  48.             {
  49.                 Console.Write("{0} ", GetTribonacci(i));
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement