Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % First star
- % I think I found a pretty little solution for the first star where I
- % avoid doing a lot of matrix manuevering.
- % Because the squares of numbers follow two separate diagonal lines
- % throughout the grid, I can deduce where my input number is located.
- % Afterwards a little addition/subtraction is needed to calculate the
- % manhatten distance to the center of the grid.
- input = 347991;
- % Square number counter
- x = 0;
- while x^2 < input
- x = x + 1;
- end
- x = x - 1;
- if mod(x,2) == 1
- if (x+1)^2 - input == input - (x^2+1)
- steps = x + 1;
- elseif (x+1)^2 - input < input - (x^2+1)
- row = abs(((x+1)^2 - (x-1)/2) - input);
- steps = (x+1)/2 + row;
- else
- col = abs((x^2+1 + (x-1)/2) - input);
- steps = (x+1)/2 + col;
- end
- else
- if (x+1)^2 - input == input - (x^2+1)
- steps = x;
- elseif (x+1)^2 - input < input - (x^2+1)
- row = abs(((x+1)^2 - x/2) - input);
- steps = x/2 + row;
- else
- col = abs((x^2+1 + x/2) - input);
- steps = x/2 + col;
- end
- end
- fprintf('Number of steps: %d\n',steps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement