Advertisement
mustafov

Crossing Sequences

Sep 3rd, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. // Crossing Sequences
  2. //We’re dealing with two sequences: the Tribonacci sequence, where every number is the sum of the previous three, and the number //spiral, defined by walking over a grid of numbers as a spiral (right, down, left, up, right, down, up, left, …) and writing down //the current number every time we take a turn. Find the first number that appears in both sequences.
  3. //Example
  4. //Let the Tribonacci sequence start with 1, 2 and 3. It will therefore contain the numbers 1, 2, 3, 6, 11, 20, 37, 68, 125, 230, //423, 778, 1431, 2632, 4841, 8904, 16377, 30122, 55403, 101902, and so on.
  5. //Also, let the number spiral start with 5 and have a step of 2; it then contains he numbers 5, 7, 9, 13, 17, 23, 29, 37, etc. //Since 37 is the first number that is both in the Tribonacci sequence and in the spiral, it is the answer.
  6.  
  7.  
  8. using System;
  9.  
  10. namespace CrossingSequences
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             int first = int.Parse(Console.ReadLine());
  17.             int second = int.Parse(Console.ReadLine());
  18.             int third = int.Parse(Console.ReadLine());
  19.             int number = int.Parse(Console.ReadLine());
  20.             int pos = int.Parse(Console.ReadLine());
  21.             int forth = 0;
  22.             bool isNotEqual = true;
  23.             int count = 0;
  24.             int remFirst = 0;
  25.             int remNumber = number;
  26.             int remPos = pos;
  27.             while (isNotEqual)
  28.             {
  29.                 int counter = 0;
  30.                 number = remNumber;
  31.                 pos = remPos;
  32.                 count = remPos;
  33.                 forth = first + second + third;
  34.                 remFirst = first;
  35.                 first = second;
  36.                 second = third;
  37.                 third = forth;
  38.                 while (isNotEqual)
  39.                 {
  40.                     number = number;
  41.                     if (remFirst == number)
  42.                     {
  43.                         isNotEqual = false;
  44.                         break;
  45.                     }
  46.                     if (first == number)
  47.                     {
  48.                         isNotEqual = false;
  49.                         break;
  50.                     }
  51.                     if (second == number)
  52.                     {
  53.                         isNotEqual = false;
  54.                         break;
  55.                     }
  56.  
  57.                     if (forth == number)
  58.                     {
  59.                         isNotEqual = false;
  60.                         break;
  61.                     }
  62.                     number += pos;
  63.                     if (number > forth)
  64.                     {
  65.                         break;
  66.                     }
  67.                     count++;
  68.                     counter++;
  69.                     if (count % remPos == 0 && counter == 2)
  70.                     {
  71.                         pos = count;
  72.                         counter = 0;
  73.                     }
  74.                 }
  75.                 if (number >= 1000000 || number < 1 || forth < 1 || forth >= 1000000)
  76.                 {
  77.                     Console.WriteLine("No"); break;
  78.                 }
  79.  
  80.             }
  81.             if ((number <= 1000000 && number >= 1) && (forth >= 1 && forth <= 1000000))
  82.             {
  83.                 Console.WriteLine(number);
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement