Getting perfect squares, differently
Create a program that gets all perfect squares starting from 0
or 1
without using any methods of multiplication (repetitive addition) and exponentiation (repetitive multiplication). Output them for validation.
Things such as built-ins/functions related to these unusable methods can't be used either. pow(x,y)
and x**y
are examples of these unusable methods.
A simple, yet tricky activity! Being code-golf, the shortest program in each programming language wins!
JavaScript (V8), 28 bytes …
3y ago
Japt, 9 bytes No infinite l …
3y ago
[Husk], 3 bytes ∫İ1 …
3y ago
[PHP], 57 bytes <?p …
3y ago
[Lua], 41 40 bytes …
3y ago
[Python 3], 34 bytes …
3y ago
[C (clang)], 47 45 43 bytes …
3y ago
7 answers
Husk, 3 bytes
∫İ1
Scan from left over all odd numbers with addition.
Uses this formula:
$$ n^2 = \sum_{k=1}^n(2k-1) $$
0 comment threads
Japt, 9 bytes
No infinite lists in Japt so we'll have to go with a recursive solution.
ßOpTµJJµ2
ßOpTµJJµ2
ß :Recursive call with (irrelevant) argument
Op : Output with trailing newline (returns undefined and second argument is ignored)
TµJ : Decrement T (initially 0) by J (initially -1)
Jµ2 : Decrement J by 2
0 comment threads
Python 3, 34 bytes
x=0;y=1
while 1:x+=y;print(x);y+=2
1
is the int
equivalent to True
, which helps save bytes.
0 comment threads
PHP, 57 bytes
<?php $x=0;$y=1;while(1){echo(string)($x+=$y)." ";$y+=2;}
PHP's a nice language, I tell ya.
2 comment threads