Skip to main content
8 of 8
added 223 characters in body
dwana
  • 551
  • 4
  • 8

Calculate the square of an integer with restrictions

The Puzzle

You have to write a program or function p(n) that returns the square of what is entered into it and you may assume that the input is a non-negative integer. Put in simpler terms, return n2.

Rules

  1. You are not allowed to use * or / (or any other power or square root operator, such as POW or SQRT, if your language contains such functions)
  2. You may not use a loop, or use a constructions that is similar to a loop. Example of loop like constructions are GOTO and recursion.

Example

Function p(n)
Dim r()
ReDim r(n)
p = Len(Join(r, Space(n)))
End Function

Be creative and (ab)use the functions and features given to you by your language of choice.

*edit

Loop like structures are loops that allow you to repeat 1 or more instructions

-if you could add an stdout "1" to your code and you would end up with repeating that output n times, it will count as a loop

dwana
  • 551
  • 4
  • 8