Find square difference: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL 68}}: Corrected comments and typos) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">L(n) 1.. |
||
I n^2 - (n - 1)^2 > 1000 |
I n^2 - (n - 1)^2 > 1000 |
||
print(n) |
print(n) |
||
L.break</ |
L.break</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 18: | Line 18: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Find_Square_Difference is |
procedure Find_Square_Difference is |
||
Line 34: | Line 34: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Find_Square_Difference;</ |
end Find_Square_Difference;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 42: | Line 42: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Also shows the least positive integer where the difference between n^2 and (n-1)^2 is greater than 32 000 and 2 000 000 000. |
Also shows the least positive integer where the difference between n^2 and (n-1)^2 is greater than 32 000 and 2 000 000 000. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 # |
||
[]INT test = ( 1 000, 32 000, 2 000 000 000 ); |
[]INT test = ( 1 000, 32 000, 2 000 000 000 ); |
||
FOR i FROM LWB test TO UPB test DO |
FOR i FROM LWB test TO UPB test DO |
||
Line 54: | Line 54: | ||
) |
) |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 63: | Line 63: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="pascal">begin % find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 % |
||
integer requiredDifference; |
integer requiredDifference; |
||
requiredDifference := 1000; |
requiredDifference := 1000; |
||
Line 70: | Line 70: | ||
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1 |
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1 |
||
) |
) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 78: | Line 78: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">i: new 1 |
||
while ø [ |
while ø [ |
||
if 1000 < (i^2)-(dec i)^2 |
if 1000 < (i^2)-(dec i)^2 |
||
Line 84: | Line 84: | ||
inc 'i |
inc 'i |
||
] |
] |
||
print i</ |
print i</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 91: | Line 91: | ||
=={{header|Asymptote}}== |
=={{header|Asymptote}}== |
||
< |
<syntaxhighlight lang="asymptote">int fpow(int n) { |
||
int i = 0; |
int i = 0; |
||
while (i^2 - (i-1)^2 < n) ++i; |
while (i^2 - (i-1)^2 < n) ++i; |
||
Line 97: | Line 97: | ||
} |
} |
||
write(fpow(1000));</ |
write(fpow(1000));</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">while ((n:=A_Index)**2 - (n-1)**2 < 1000) |
||
continue |
continue |
||
MsgBox % result := n</ |
MsgBox % result := n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f FIND_SQUARE_DIFFERENCE.AWK |
# syntax: GAWK -f FIND_SQUARE_DIFFERENCE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 117: | Line 117: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 126: | Line 126: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="freebasic">function fpow(n) |
||
i = 0 |
i = 0 |
||
while i^2 - (i-1)^2 < n |
while i^2 - (i-1)^2 < n |
||
Line 135: | Line 135: | ||
print fpow(1001) |
print fpow(1001) |
||
end</ |
end</syntaxhighlight> |
||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
< |
<syntaxhighlight lang="purebasic">Procedure fpow(n.i) |
||
Define i.i |
Define i.i |
||
While Pow(i, 2) - Pow((i-1), 2) < n |
While Pow(i, 2) - Pow((i-1), 2) < n |
||
Line 149: | Line 149: | ||
Print(Str(fpow(1001))) |
Print(Str(fpow(1001))) |
||
Input() |
Input() |
||
CloseConsole()</ |
CloseConsole()</syntaxhighlight> |
||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
< |
<syntaxhighlight lang="qbasic">FUNCTION fpow (n) |
||
WHILE (i * i) - ((i - 1) * (i - 1)) < n |
WHILE (i * i) - ((i - 1) * (i - 1)) < n |
||
i = i + 1 |
i = i + 1 |
||
Line 159: | Line 159: | ||
END FUNCTION |
END FUNCTION |
||
PRINT fpow(1001)</ |
PRINT fpow(1001)</syntaxhighlight> |
||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
< |
<syntaxhighlight lang="lb">function fpow(n) |
||
while i^2-(i-1)^2 < n |
while i^2-(i-1)^2 < n |
||
i = i+1 |
i = i+1 |
||
Line 169: | Line 169: | ||
end function |
end function |
||
print fpow(1001)</ |
print fpow(1001)</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang="qbasic">FUNCTION fpow (n) |
||
DO WHILE i ^ 2 - (i - 1) ^ 2 < n |
DO WHILE i ^ 2 - (i - 1) ^ 2 < n |
||
LET i = i + 1 |
LET i = i + 1 |
||
Line 180: | Line 180: | ||
PRINT fpow(1001) |
PRINT fpow(1001) |
||
END</ |
END</syntaxhighlight> |
||
==={{Header|Tiny BASIC}}=== |
==={{Header|Tiny BASIC}}=== |
||
< |
<syntaxhighlight lang="qbasic"> LET N = 1001 |
||
LET I = 0 |
LET I = 0 |
||
Line 191: | Line 191: | ||
GOTO 10 |
GOTO 10 |
||
20 PRINT I |
20 PRINT I |
||
END</ |
END</syntaxhighlight> |
||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="freebasic">sub fpow(n) |
||
while i^2 - (i-1)^2 < n |
while i^2 - (i-1)^2 < n |
||
i = i + 1 |
i = i + 1 |
||
Line 202: | Line 202: | ||
print fpow(1001) |
print fpow(1001) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 218: | Line 218: | ||
printf( "%d\n", f(1000) ); |
printf( "%d\n", f(1000) ); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
Line 224: | Line 224: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
The C solution is also idomatic in C++. An alterate approach is to use Ranges from C++20. |
The C solution is also idomatic in C++. An alterate approach is to use Ranges from C++20. |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <ranges> |
#include <ranges> |
||
Line 236: | Line 236: | ||
std::cout << answer.front() << '\n'; |
std::cout << answer.front() << '\n'; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 243: | Line 243: | ||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">import 'dart:math'; |
||
int leastSquare(int gap) { |
int leastSquare(int gap) { |
||
Line 255: | Line 255: | ||
void main() { |
void main() { |
||
print(leastSquare(1000)); |
print(leastSquare(1000)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let n=1000 in printfn $"%d{((n+1)/2)+1}" |
let n=1000 in printfn $"%d{((n+1)/2)+1}" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 270: | Line 270: | ||
The difference between squares is the odd numbers, so ls(n)=⌈n/2+1⌉. |
The difference between squares is the odd numbers, so ls(n)=⌈n/2+1⌉. |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: math math.functions prettyprint ; |
||
: least-sq ( m -- n ) 2 / 1 + ceiling ; |
: least-sq ( m -- n ) 2 / 1 + ceiling ; |
||
1000 least-sq .</ |
1000 least-sq .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 281: | Line 281: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
<lang>Func F(n) = |
<syntaxhighlight lang="text">Func F(n) = |
||
i:=0; |
i:=0; |
||
while i^2-(i-1)^2<n do i:=i+1 od; i.; |
while i^2-(i-1)^2<n do i:=i+1 od; i.; |
||
!!F(1000);</ |
!!F(1000);</syntaxhighlight> |
||
{{out}}<pre>501</pre> |
{{out}}<pre>501</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function fpow(n as uinteger) as uinteger |
||
dim as uinteger i |
dim as uinteger i |
||
while i^2-(i-1)^2 < n |
while i^2-(i-1)^2 < n |
||
Line 297: | Line 297: | ||
end function |
end function |
||
print fpow(1001)</ |
print fpow(1001)</syntaxhighlight> |
||
{{out}}<pre>501</pre> |
{{out}}<pre>501</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 314: | Line 314: | ||
func main() { |
func main() { |
||
fmt.Println(squareDiff(1000)) |
fmt.Println(squareDiff(1000)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 323: | Line 323: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
The sequence of differences between successive squares is the sequence of odd numbers. |
The sequence of differences between successive squares is the sequence of odd numbers. |
||
< |
<syntaxhighlight lang="haskell">import Data.List (findIndex) |
||
f = succ . flip div 2 |
f = succ . flip div 2 |
||
Line 333: | Line 333: | ||
Just i = succ <$> findIndex (> n) [1, 3 ..] |
Just i = succ <$> findIndex (> n) [1, 3 ..] |
||
main = mapM_ print $ [f, g] <*> [1000]</ |
main = mapM_ print $ [f, g] <*> [1000]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>501 |
<pre>501 |
||
Line 345: | Line 345: | ||
At the risk of hastening RC's demise, one could offer a jq solution like so: |
At the risk of hastening RC's demise, one could offer a jq solution like so: |
||
< |
<syntaxhighlight lang="jq">first( range(1; infinite) | select( 2 * . - 1 > 1000 ) )</syntaxhighlight> |
||
Or, for anyone envious of Bitcoin's contribution to global warming: |
Or, for anyone envious of Bitcoin's contribution to global warming: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
first( range(1; infinite) | select( .*. - ((.-1) | .*.) > 1000 ) ) |
first( range(1; infinite) | select( .*. - ((.-1) | .*.) > 1000 ) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 356: | Line 356: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">julia> findfirst(n -> n*n - (n-1)*(n-1) > 1000, 1:1_000_000) |
||
501 |
501 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Pari/GP}}== |
=={{header|Pari/GP}}== |
||
< |
<syntaxhighlight lang="parigp">F(n)=i=0;while(i^2-(i-1)^2<n,i=i+1);return(i); |
||
print(F(1000))</ |
print(F(1000))</syntaxhighlight> |
||
{{out}}<pre>501</pre> |
{{out}}<pre>501</pre> |
||
Line 386: | Line 386: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Least_square |
use strict; # https://rosettacode.org/wiki/Least_square |
||
Line 393: | Line 393: | ||
my $n = 1; |
my $n = 1; |
||
$n++ until $n ** 2 - ($n-1) ** 2 > 1000; |
$n++ until $n ** 2 - ($n-1) ** 2 > 1000; |
||
print "$n\n";</ |
print "$n\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 401: | Line 401: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<small>''Essentially Wren equivalent, but explained in excruciating detail especially for enyone that evidently needs elp, said Eeyore.''</small> |
<small>''Essentially Wren equivalent, but explained in excruciating detail especially for enyone that evidently needs elp, said Eeyore.''</small> |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""" |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""" |
||
Line 412: | Line 412: | ||
n = %d |
n = %d |
||
"""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">))</span> |
"""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 424: | Line 424: | ||
</pre> |
</pre> |
||
Or if you prefer, same output: |
Or if you prefer, same output: |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- equation</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- equation</span> |
||
Line 441: | Line 441: | ||
<span style="color: #000000;">p</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"2*"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"500.5"</span><span style="color: #0000FF;">}))</span> <span style="color: #000080;font-style:italic;">-- divide by 2</span> |
<span style="color: #000000;">p</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"2*"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"500.5"</span><span style="color: #0000FF;">}))</span> <span style="color: #000080;font-style:italic;">-- divide by 2</span> |
||
<span style="color: #000000;">p</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"> 500.5"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"= %d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">))))</span> <span style="color: #000080;font-style:italic;">-- solve</span> |
<span style="color: #000000;">p</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"> 500.5"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"= %d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">))))</span> <span style="color: #000080;font-style:italic;">-- solve</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
or even: |
or even: |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (user defined types are not implicitly called)</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (user defined types are not implicitly called)</span> |
||
<span style="color: #008080;">type</span> <span style="color: #000000;">pstring</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">type</span> |
<span style="color: #008080;">type</span> <span style="color: #000000;">pstring</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">type</span> |
||
Line 457: | Line 457: | ||
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"2*"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"500.5"</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- divide by 2</span> |
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"2*"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"500.5"</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- divide by 2</span> |
||
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"> 500.5"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"= %d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">)))</span> <span style="color: #000080;font-style:italic;">-- solve</span> |
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"> 500.5"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"= %d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500.5</span><span style="color: #0000FF;">)))</span> <span style="color: #000080;font-style:italic;">-- solve</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator. |
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator. |
||
<br>Note that the original 8080 PL/M compiler only supports 8 and 16 bit unisgned numbers. |
<br>Note that the original 8080 PL/M compiler only supports 8 and 16 bit unisgned numbers. |
||
< |
<syntaxhighlight lang="pli">100H: /* FIND THE LEAST +VE N WHERE N SQUARED - (N-1) SQUARED > 1000 */ |
||
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */ |
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */ |
||
Line 497: | Line 497: | ||
CALL PRINT$LEAST( 65000 ); |
CALL PRINT$LEAST( 65000 ); |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 506: | Line 506: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
import math |
import math |
||
print("working...") |
print("working...") |
||
Line 524: | Line 524: | ||
print("done...") |
print("done...") |
||
print() |
print() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 534: | Line 534: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ dup * ] is squared ( n --> n ) |
||
0 |
0 |
||
Line 541: | Line 541: | ||
over 1 - squared - |
over 1 - squared - |
||
1000 > until ] |
1000 > until ] |
||
echo</ |
echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 551: | Line 551: | ||
Noting that a²-b² ≡ (a+b)(a-b), and that in this instance a = b+1. |
Noting that a²-b² ≡ (a+b)(a-b), and that in this instance a = b+1. |
||
< |
<syntaxhighlight lang="quackery"> 1000 2 / 1+ echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 558: | Line 558: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>say first { $_² - ($_-1)² > 1000 }, ^Inf;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 581: | Line 581: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 590: | Line 590: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var N = 1000 |
||
# Binary search |
# Binary search |
||
Line 603: | Line 603: | ||
assert_eq(n, m) |
assert_eq(n, m) |
||
say "#{n}^2 - #{n-1}^2 = #{n**2 - (n-1)**2}"</ |
say "#{n}^2 - #{n-1}^2 = #{n**2 - (n-1)**2}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 610: | Line 610: | ||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
< |
<syntaxhighlight lang="verilog">module main; |
||
integer i, n; |
integer i, n; |
||
Line 620: | Line 620: | ||
$finish ; |
$finish ; |
||
end |
end |
||
endmodule</ |
endmodule</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
The solution '''n''' for some non-negative integer '''k''' needs to be such that: ''n² - (n² - 2n + 1) > k'' which reduces to: ''n > (k + 1)/2''. |
The solution '''n''' for some non-negative integer '''k''' needs to be such that: ''n² - (n² - 2n + 1) > k'' which reduces to: ''n > (k + 1)/2''. |
||
< |
<syntaxhighlight lang="ecmascript">var squareDiff = Fn.new { |k| ((k + 1) * 0.5).ceil } |
||
System.print(squareDiff.call(1000))</ |
System.print(squareDiff.call(1000))</syntaxhighlight> |
||
{{out}} |
{{out}} |