Advertisement
Pietras286

Problem Collatza

Dec 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. namespace Problem17
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int t = int.Parse(Console.ReadLine());
  8.             for (int i = 0; i < t; i++)
  9.             {
  10.                 int s = int.Parse(Console.ReadLine());
  11.                 Console.WriteLine(Oblicz(s,0));
  12.             }
  13.         }
  14.  
  15.         private static int Oblicz(int s, int n)
  16.         {
  17.             if (s == 1)
  18.             {
  19.                 return n;
  20.             }
  21.             else
  22.             {
  23.                 if (s % 2 == 1)
  24.                 {
  25.                     return Oblicz(3 * s + 1, ++n);
  26.                 }
  27.                 else
  28.                 {
  29.                     return Oblicz(s / 2, ++n);
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement