Ulam spiral

For an excercise, I needed to design an algorith that finds the coordinates of a number in the Ulam spiral//
Para un ejercicio tuve que realizar un algoritmo para encontrar las coordenadas de un numero de la espiral de Ulam

If you find this code useful, here it is//
Por si a alguien le sirve, este es el código

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2 {
    class Program {
        static void Main(string[] args) {
            int option;

            do {
                option = ShowMenu();
                switch (option) {
                    case 1:
                        Coordinates();
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            }
            while (option != 4);
        }

        static public int ShowMenu() {
            Console.WriteLine("1. Determinar coordenadas de un punto");
            Console.WriteLine("2. Determinar numero de un punto");
            Console.WriteLine("3. Mayor entero de un circulo");
            Console.WriteLine("4. Salir");
            int input = Convert.ToInt32(Console.ReadLine());
            return input;
        }

        static public void Coordinates() {

            int circle;
            int multiplier;
            int[] numbers;
            int counter = 0; ;
            int i = 0;
            int first;
            int firstX;
            int firstY;
            bool isFound = false;

            Console.WriteLine("Determinar coordenadas de un punto");
            int number = Convert.ToInt32(Console.ReadLine());
            if(number == 0) {
                Console.WriteLine("{0,0}");
            } 
            else {
                while(number > counter*8) {
                    i += 1;
                    counter += i;
                }
            }
            circle = i;
            Console.WriteLine(circle);
            counter -= i;
            Console.WriteLine(counter*8);

            first = (counter*8) + 2; //change to -1 if your spiral starts at 0
            Console.WriteLine(first);
            firstX = circle;
            firstY = (circle * -1) + 1;

            while (firstY <= circle) { Console.WriteLine("x" + firstX + "y" + firstY); if (first == number) { isFound = true; break; } else { first++; firstY++; } } Console.WriteLine(isFound); Console.WriteLine(first); Console.WriteLine("x" + firstX + "y" + firstY); if (!isFound) { first--; firstY--; while(firstX >= -circle) {
                    Console.WriteLine("x" + firstX + "y" + firstY);
                    if (first == number) {
                        isFound = true;
                        break;
                    } else {
                        first++;
                        firstX--;
                    }
                }
                Console.WriteLine(isFound);
                Console.WriteLine(first);
                Console.WriteLine("x" + firstX + "y" + firstY);
            }

            if (!isFound) {
                first--;
                firstX++;
                while (firstY >= -circle) {
                    Console.WriteLine("x" + firstX + "y" + firstY);
                    if (first == number) {
                        isFound = true;
                        break;
                    } else {
                        first++;
                        firstY--;
                    }
                }
                Console.WriteLine(isFound);
                Console.WriteLine(first);
                Console.WriteLine("x" + firstX + "y" + firstY);
            }

            if (!isFound) {
                first--;
                firstY++;
                while (firstX <= circle) {
                    Console.WriteLine("x" + firstX + "y" + firstY);
                    if (first == number) {
                        isFound = true;
                        break;
                    } else {
                        first++;
                        firstX++;
                    }
                }
                Console.WriteLine(isFound);
                Console.WriteLine(first);
                Console.WriteLine("x" + firstX + "y" + firstY);
            }

            Console.WriteLine("x" + firstX + "y" + firstY);
            Console.WriteLine("Presionar cualquier tecla vovler al menu");
            Console.WriteLine("");
            Console.ReadKey();


        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s