Find square difference: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|ALGOL 68}}: Corrected comments and typos) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7:
=={{header|11l}}==
<
I n^2 - (n - 1)^2 > 1000
print(n)
L.break</
{{out}}
Line 18:
=={{header|Ada}}==
<
procedure Find_Square_Difference is
Line 34:
end if;
end loop;
end Find_Square_Difference;</
{{out}}
<pre>
Line 42:
=={{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.
<
[]INT test = ( 1 000, 32 000, 2 000 000 000 );
FOR i FROM LWB test TO UPB test DO
Line 54:
)
OD
END</
{{out}}
<pre>
Line 63:
=={{header|ALGOL W}}==
<
integer requiredDifference;
requiredDifference := 1000;
Line 70:
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1
)
end.</
{{out}}
<pre>
Line 78:
=={{header|Arturo}}==
<
while ø [
if 1000 < (i^2)-(dec i)^2
Line 84:
inc 'i
]
print i</
{{out}}
Line 91:
=={{header|Asymptote}}==
<
int i = 0;
while (i^2 - (i-1)^2 < n) ++i;
Line 97:
}
write(fpow(1000));</
=={{header|AutoHotkey}}==
<
continue
MsgBox % result := n</
{{out}}
<pre>501</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_SQUARE_DIFFERENCE.AWK
BEGIN {
Line 117:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 126:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<
i = 0
while i^2 - (i-1)^2 < n
Line 135:
print fpow(1001)
end</
==={{header|PureBasic}}===
<
Define i.i
While Pow(i, 2) - Pow((i-1), 2) < n
Line 149:
Print(Str(fpow(1001)))
Input()
CloseConsole()</
==={{header|QBasic}}===
<
WHILE (i * i) - ((i - 1) * (i - 1)) < n
i = i + 1
Line 159:
END FUNCTION
PRINT fpow(1001)</
==={{header|Run BASIC}}===
<
while i^2-(i-1)^2 < n
i = i+1
Line 169:
end function
print fpow(1001)</
==={{header|True BASIC}}===
<
DO WHILE i ^ 2 - (i - 1) ^ 2 < n
LET i = i + 1
Line 180:
PRINT fpow(1001)
END</
==={{Header|Tiny BASIC}}===
<
LET I = 0
Line 191:
GOTO 10
20 PRINT I
END</
==={{header|Yabasic}}===
<
while i^2 - (i-1)^2 < n
i = i + 1
Line 202:
print fpow(1001)
end</
=={{header|C}}==
<
#include<stdlib.h>
Line 218:
printf( "%d\n", f(1000) );
return 0;
}</
{{out}}
<pre>501</pre>
Line 224:
=={{header|C++}}==
The C solution is also idomatic in C++. An alterate approach is to use Ranges from C++20.
<
#include <ranges>
Line 236:
std::cout << answer.front() << '\n';
}
</syntaxhighlight>
{{out}}
<pre>
Line 243:
=={{header|Dart}}==
<
int leastSquare(int gap) {
Line 255:
void main() {
print(leastSquare(1000));
}</
{{out}}
<pre>501</pre>
=={{header|F_Sharp|F#}}==
<
let n=1000 in printfn $"%d{((n+1)/2)+1}"
</syntaxhighlight>
{{out}}
<pre>
Line 270:
The difference between squares is the odd numbers, so ls(n)=⌈n/2+1⌉.
{{works with|Factor|0.99 2021-06-02}}
<
: least-sq ( m -- n ) 2 / 1 + ceiling ;
1000 least-sq .</
{{out}}
<pre>
Line 281:
=={{header|Fermat}}==
<syntaxhighlight lang="text">Func F(n) =
i:=0;
while i^2-(i-1)^2<n do i:=i+1 od; i.;
!!F(1000);</
{{out}}<pre>501</pre>
=={{header|FreeBASIC}}==
<
dim as uinteger i
while i^2-(i-1)^2 < n
Line 297:
end function
print fpow(1001)</
{{out}}<pre>501</pre>
=={{header|Go}}==
<
import (
Line 314:
func main() {
fmt.Println(squareDiff(1000))
}</
{{out}}
Line 323:
=={{header|Haskell}}==
The sequence of differences between successive squares is the sequence of odd numbers.
<
f = succ . flip div 2
Line 333:
Just i = succ <$> findIndex (> n) [1, 3 ..]
main = mapM_ print $ [f, g] <*> [1000]</
{{Out}}
<pre>501
Line 345:
At the risk of hastening RC's demise, one could offer a jq solution like so:
<
Or, for anyone envious of Bitcoin's contribution to global warming:
<syntaxhighlight lang="jq">
first( range(1; infinite) | select( .*. - ((.-1) | .*.) > 1000 ) )
</syntaxhighlight>
{{out}}
<pre>
Line 356:
=={{header|Julia}}==
<
501
</syntaxhighlight>
=={{header|Pari/GP}}==
<
print(F(1000))</
{{out}}<pre>501</pre>
Line 386:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Least_square
Line 393:
my $n = 1;
$n++ until $n ** 2 - ($n-1) ** 2 > 1000;
print "$n\n";</
{{out}}
<pre>
Line 401:
=={{header|Phix}}==
<small>''Essentially Wren equivalent, but explained in excruciating detail especially for enyone that evidently needs elp, said Eeyore.''</small>
<!--<
<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;">"""
Line 412:
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>
<!--</
{{out}}
<pre>
Line 424:
</pre>
Or if you prefer, same output:
<!--<
<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>
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</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>
<!--</
or even:
<!--<
<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>
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</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>
<!--</
=={{header|PL/M}}==
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.
<
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
Line 497:
CALL PRINT$LEAST( 65000 );
EOF</
{{out}}
<pre>
Line 506:
=={{header|Python}}==
<
import math
print("working...")
Line 524:
print("done...")
print()
</syntaxhighlight>
{{out}}
<pre>
Line 534:
=={{header|Quackery}}==
<
0
Line 541:
over 1 - squared -
1000 > until ]
echo</
{{out}}
Line 551:
Noting that a²-b² ≡ (a+b)(a-b), and that in this instance a = b+1.
<
{{out}}
Line 558:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre>501</pre>
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 581:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 590:
=={{header|Sidef}}==
<
# Binary search
Line 603:
assert_eq(n, m)
say "#{n}^2 - #{n-1}^2 = #{n**2 - (n-1)**2}"</
{{out}}
<pre>
Line 610:
=={{header|Verilog}}==
<
integer i, n;
Line 620:
$finish ;
end
endmodule</
=={{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''.
<
System.print(squareDiff.call(1000))</
{{out}}
|