Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Crossing Sequences
- //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.
- //Example
- //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.
- //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.
- using System;
- namespace CrossingSequences
- {
- class Program
- {
- static void Main(string[] args)
- {
- int first = int.Parse(Console.ReadLine());
- int second = int.Parse(Console.ReadLine());
- int third = int.Parse(Console.ReadLine());
- int number = int.Parse(Console.ReadLine());
- int pos = int.Parse(Console.ReadLine());
- int forth = 0;
- bool isNotEqual = true;
- int count = 0;
- int remFirst = 0;
- int remNumber = number;
- int remPos = pos;
- while (isNotEqual)
- {
- int counter = 0;
- number = remNumber;
- pos = remPos;
- count = remPos;
- forth = first + second + third;
- remFirst = first;
- first = second;
- second = third;
- third = forth;
- while (isNotEqual)
- {
- number = number;
- if (remFirst == number)
- {
- isNotEqual = false;
- break;
- }
- if (first == number)
- {
- isNotEqual = false;
- break;
- }
- if (second == number)
- {
- isNotEqual = false;
- break;
- }
- if (forth == number)
- {
- isNotEqual = false;
- break;
- }
- number += pos;
- if (number > forth)
- {
- break;
- }
- count++;
- counter++;
- if (count % remPos == 0 && counter == 2)
- {
- pos = count;
- counter = 0;
- }
- }
- if (number >= 1000000 || number < 1 || forth < 1 || forth >= 1000000)
- {
- Console.WriteLine("No"); break;
- }
- }
- if ((number <= 1000000 && number >= 1) && (forth >= 1 && forth <= 1000000))
- {
- Console.WriteLine(number);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement