Advertisement
Guest User

Untitled

a guest
Aug 13th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.Write("Enter i: ");
  13.             int i = int.Parse(Console.ReadLine());
  14.             Console.Write("Enter j: ");
  15.             int j = int.Parse(Console.ReadLine());
  16.             int cycle = 0;
  17.             int temp;
  18.  
  19.             for (int n = j; n >= i; n = n - 1)
  20.             {
  21.                 temp = cycles(n);
  22.                 if (temp > cycle)
  23.                 {
  24.                     cycle = temp;
  25.                 }
  26.             }
  27.             Console.Write("i = {0}, j = {1}, cycle = {2}", i, j, cycle);
  28.             Console.ReadKey();
  29.         }
  30.  
  31.         static int cycles(int n)
  32.         {
  33.             int temp = 1;
  34.             while (n != 1)
  35.             {
  36.                 if (n % 2 == 0)
  37.                 {
  38.                     n = n / 2;
  39.                 }
  40.                 else
  41.                 {
  42.                     n = n * 3 + 1;
  43.                 }
  44.                 temp++;
  45.             }
  46.             return temp;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement