Skip to main content
Commonmark migration
Source Link

Forth (gforth), 76 bytes

: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ; 

Try it online!

Explanation

###Explanation StartsStarts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k

###Code Explanation

Code Explanation

: f \ start a new word definition 1 \ place a counter on the stack, start it at 1 begin \ start and indefinite loop 1+ \ add 1 to the counter dup s>f \ convert a copy of the counter to a float fsqrt \ get the square root of the counter fdup fround f- \ get the difference between the square root and the next closes integer fabs fdup \ get the absolute value of the result and duplicate f0> \ check if the result is greater than 0 (not perfect square) fover f< \ bring k to the top of the float stack and check if the sqrt is less than k * \ multiply the two results (shorter "and" in this case) until \ end loop if result ("and" of both conditions) is true ; \ end word definition 

Forth (gforth), 76 bytes

: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ; 

Try it online!

###Explanation Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k

###Code Explanation

: f \ start a new word definition 1 \ place a counter on the stack, start it at 1 begin \ start and indefinite loop 1+ \ add 1 to the counter dup s>f \ convert a copy of the counter to a float fsqrt \ get the square root of the counter fdup fround f- \ get the difference between the square root and the next closes integer fabs fdup \ get the absolute value of the result and duplicate f0> \ check if the result is greater than 0 (not perfect square) fover f< \ bring k to the top of the float stack and check if the sqrt is less than k * \ multiply the two results (shorter "and" in this case) until \ end loop if result ("and" of both conditions) is true ; \ end word definition 

Forth (gforth), 76 bytes

: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ; 

Try it online!

Explanation

Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k

Code Explanation

: f \ start a new word definition 1 \ place a counter on the stack, start it at 1 begin \ start and indefinite loop 1+ \ add 1 to the counter dup s>f \ convert a copy of the counter to a float fsqrt \ get the square root of the counter fdup fround f- \ get the difference between the square root and the next closes integer fabs fdup \ get the absolute value of the result and duplicate f0> \ check if the result is greater than 0 (not perfect square) fover f< \ bring k to the top of the float stack and check if the sqrt is less than k * \ multiply the two results (shorter "and" in this case) until \ end loop if result ("and" of both conditions) is true ; \ end word definition 
Source Link
reffu
  • 2k
  • 11
  • 11

Forth (gforth), 76 bytes

: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ; 

Try it online!

###Explanation Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k

###Code Explanation

: f \ start a new word definition 1 \ place a counter on the stack, start it at 1 begin \ start and indefinite loop 1+ \ add 1 to the counter dup s>f \ convert a copy of the counter to a float fsqrt \ get the square root of the counter fdup fround f- \ get the difference between the square root and the next closes integer fabs fdup \ get the absolute value of the result and duplicate f0> \ check if the result is greater than 0 (not perfect square) fover f< \ bring k to the top of the float stack and check if the sqrt is less than k * \ multiply the two results (shorter "and" in this case) until \ end loop if result ("and" of both conditions) is true ; \ end word definition