Find square difference: Difference between revisions
Find square difference in Run BASIC |
Add SmallBASIC |
||
(29 intermediate revisions by 17 users not shown) | |||
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 16: | Line 16: | ||
501 |
501 |
||
</pre> |
</pre> |
||
=={{header|AArch64 Assembly}}== |
|||
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }} |
|||
<syntaxhighlight lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
|||
/* program diffsquare64.s */ |
|||
/************************************/ |
|||
/* Constantes */ |
|||
/************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeConstantesARM64.inc" |
|||
.equ MAXI, 10000 |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessNotFind: .asciz "No soluce !! " |
|||
szMessResult: .asciz "Nombre = " |
|||
szMessStart: .asciz "Program 64 bits start.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: // entry of program |
|||
ldr x0,qAdrszMessStart |
|||
bl affichageMess |
|||
mov x5,#2 // init start number |
|||
ldr x3,iMAXI |
|||
1: // begin loop |
|||
mul x2,x5,x5 // n * n |
|||
sub x4,x5,#1 // n - 1 |
|||
mul x6,x4,x4 // (n - 1) * (n - 1) |
|||
sub x0,x2,x6 // difference |
|||
cmp x0,#1000 // > 1000 |
|||
bgt 2f |
|||
add x5,x5,#1 // increment number |
|||
cmp x5,x3 // maxi limit ? |
|||
blt 1b |
|||
ldr x0,qAdrszMessNotFind // display not find |
|||
bl affichageMess |
|||
b 100f |
|||
2: // display result |
|||
mov x0,x5 |
|||
ldr x1,qAdrsZoneConv |
|||
bl conversion10 // decimal conversion |
|||
mov x0,#3 // number string to display |
|||
ldr x1,qAdrszMessResult |
|||
ldr x2,qAdrsZoneConv // insert conversion in message |
|||
ldr x3,qAdrszCarriageReturn |
|||
bl displayStrings // display message |
|||
100: // standard end of the program |
|||
mov x0, #0 // return code |
|||
mov x8,EXIT |
|||
svc #0 // perform the system call |
|||
qAdrszCarriageReturn: .quad szCarriageReturn |
|||
qAdrsZoneConv: .quad sZoneConv |
|||
qAdrszMessResult: .quad szMessResult |
|||
qAdrszMessNotFind: .quad szMessNotFind |
|||
qAdrszMessStart: .quad szMessStart |
|||
iMAXI: .quad MAXI |
|||
/***************************************************/ |
|||
/* display multi strings */ |
|||
/* new version 24/05/2023 */ |
|||
/***************************************************/ |
|||
/* x0 contains number strings address */ |
|||
/* x1 address string1 */ |
|||
/* x2 address string2 */ |
|||
/* x3 address string3 */ |
|||
/* x4 address string4 */ |
|||
/* x5 address string5 */ |
|||
/* x6 address string5 */ |
|||
displayStrings: // INFO: displayStrings |
|||
stp x7,lr,[sp,-16]! // save registers |
|||
stp x2,fp,[sp,-16]! // save registers |
|||
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes) |
|||
mov x7,x0 // save strings number |
|||
cmp x7,#0 // 0 string -> end |
|||
ble 100f |
|||
mov x0,x1 // string 1 |
|||
bl affichageMess |
|||
cmp x7,#1 // number > 1 |
|||
ble 100f |
|||
mov x0,x2 |
|||
bl affichageMess |
|||
cmp x7,#2 |
|||
ble 100f |
|||
mov x0,x3 |
|||
bl affichageMess |
|||
cmp x7,#3 |
|||
ble 100f |
|||
mov x0,x4 |
|||
bl affichageMess |
|||
cmp x7,#4 |
|||
ble 100f |
|||
mov x0,x5 |
|||
bl affichageMess |
|||
cmp x7,#5 |
|||
ble 100f |
|||
mov x0,x6 |
|||
bl affichageMess |
|||
100: |
|||
ldp x2,fp,[sp],16 // restaur registers |
|||
ldp x7,lr,[sp],16 // restaur registers |
|||
ret |
|||
/***************************************************/ |
|||
/* ROUTINES INCLUDE */ |
|||
/***************************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeARM64.inc" |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Program 64 bits start. |
|||
Nombre = 501 |
|||
</pre> |
|||
=={{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 158: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Find_Square_Difference;</ |
end Find_Square_Difference;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 42: | Line 166: | ||
=={{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 # |
||
# shows the smalled square n where n^2 - (n-1)^2 is greater than required difference # |
|||
[]INT test = ( 1 000, 32 000, 2 000 000 000 ); |
|||
# n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 # |
|||
FOR i FROM LWB test TO UPB test DO |
|||
# so 2n - 1 > required difference or n > ( required difference + 1 ) / 2 # |
|||
PROC show least square = ( INT required difference )VOID: |
|||
# n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 # |
|||
# so 2n - 1 > reuired difference or n > reuired difference / 2 # |
|||
print( ( "Smallest n where n^2 - (n-1)^2 is > ", whole( required difference, -12 ) |
print( ( "Smallest n where n^2 - (n-1)^2 is > ", whole( required difference, -12 ) |
||
, " is: ", whole( ( ( required difference + 1 ) OVER 2 ) + 1, -12 ) |
, " is: ", whole( ( ( required difference + 1 ) OVER 2 ) + 1, -12 ) |
||
, newline |
, newline |
||
) |
) |
||
) |
); |
||
show least square( 1 000 ); |
|||
OD |
|||
show least square( 32 000 ); |
|||
END</lang> |
|||
show least square( 2 000 000 000 ) |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 63: | Line 189: | ||
=={{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 196: | ||
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1 |
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1 |
||
) |
) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501 |
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501 |
||
</pre> |
</pre> |
||
=={{header|ARM Assembly}}== |
|||
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}} |
|||
<syntaxhighlight lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
|||
/* program diffsquare.s */ |
|||
/* REMARK 1 : this program use routines in a include file |
|||
=={{header|Arturo}}== |
|||
see task Include a file language arm assembly |
|||
for the routine affichageMess conversion10 |
|||
see at end of this program the instruction include */ |
|||
/* for constantes see task include a file in arm assembly */ |
|||
/************************************/ |
|||
/* Constantes */ |
|||
/************************************/ |
|||
.include "../constantes.inc" |
|||
.equ MAXI, 10000 |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessNotFind: .asciz "No soluce !! " |
|||
szMessResult: .asciz "Nombre = " |
|||
szMessStart: .asciz "Program 32 bits start.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: @ entry of program |
|||
ldr r0,iAdrszMessStart |
|||
bl affichageMess |
|||
mov r5,#2 @ init start number |
|||
ldr r3,iMAXI |
|||
1: @ begin loop |
|||
mul r2,r5,r5 @ n * n |
|||
sub r4,r5,#1 @ n - 1 |
|||
mul r6,r4,r4 @ (n - 1) * (n - 1) |
|||
sub r0,r2,r6 @ difference |
|||
cmp r0,#1000 @ > 1000 |
|||
bgt 2f |
|||
add r5,r5,#1 @ increment number |
|||
cmp r5,r3 @ maxi limit ? |
|||
blt 1b |
|||
ldr r0,iAdrszMessNotFind @ display not find |
|||
bl affichageMess |
|||
b 100f |
|||
2: @ display result |
|||
mov r0,r5 |
|||
ldr r1,iAdrsZoneConv |
|||
bl conversion10 @ decimal conversion |
|||
mov r3,#0 @ zero final |
|||
strb r3,[r1,r0] |
|||
mov r0,#3 @ number string to display |
|||
ldr r1,iAdrszMessResult |
|||
ldr r2,iAdrsZoneConv @ insert conversion in message |
|||
ldr r3,iAdrszCarriageReturn |
|||
bl displayStrings @ display message |
|||
100: @ standard end of the program |
|||
mov r0, #0 @ return code |
|||
mov r7, #EXIT @ request to exit program |
|||
svc #0 @ perform the system call |
|||
iAdrszCarriageReturn: .int szCarriageReturn |
|||
iAdrsZoneConv: .int sZoneConv |
|||
iAdrszMessResult: .int szMessResult |
|||
iAdrszMessNotFind: .int szMessNotFind |
|||
iAdrszMessStart: .int szMessStart |
|||
iMAXI: .int MAXI |
|||
/***************************************************/ |
|||
/* display multi strings */ |
|||
/***************************************************/ |
|||
/* r0 contains number strings address */ |
|||
/* r1 address string1 */ |
|||
/* r2 address string2 */ |
|||
/* r3 address string3 */ |
|||
/* other address on the stack */ |
|||
/* thinck to add number other address * 4 to add to the stack */ |
|||
displayStrings: @ INFO: displayStrings |
|||
push {r1-r4,fp,lr} @ save des registres |
|||
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes) |
|||
mov r4,r0 @ save strings number |
|||
cmp r4,#0 @ 0 string -> end |
|||
ble 100f |
|||
mov r0,r1 @ string 1 |
|||
bl affichageMess |
|||
cmp r4,#1 @ number > 1 |
|||
ble 100f |
|||
mov r0,r2 |
|||
bl affichageMess |
|||
cmp r4,#2 |
|||
ble 100f |
|||
mov r0,r3 |
|||
bl affichageMess |
|||
cmp r4,#3 |
|||
ble 100f |
|||
mov r3,#3 |
|||
sub r2,r4,#4 |
|||
1: @ loop extract address string on stack |
|||
ldr r0,[fp,r2,lsl #2] |
|||
bl affichageMess |
|||
subs r2,#1 |
|||
bge 1b |
|||
100: |
|||
pop {r1-r4,fp,pc} |
|||
/***************************************************/ |
|||
<lang rebol>i: new 1 |
|||
/* ROUTINES INCLUDE */ |
|||
/***************************************************/ |
|||
.include "../affichage.inc" |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Program 32 bits start. |
|||
Nombre = 501 |
|||
</pre> |
|||
=={{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 332: | ||
inc 'i |
inc 'i |
||
] |
] |
||
print i</ |
print i</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 91: | Line 339: | ||
=={{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 345: | ||
} |
} |
||
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 365: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
501 |
501 |
||
</pre> |
</pre> |
||
=={{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 382: | ||
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 396: | ||
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 406: | ||
END FUNCTION |
END FUNCTION |
||
PRINT fpow(1001)</ |
PRINT fpow(1001)</syntaxhighlight> |
||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
<syntaxhighlight lang="lb">function fpow(n) |
|||
{{works with|QBasic}} |
|||
<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 170: | Line 416: | ||
end function |
end function |
||
print fpow(1001)</ |
print fpow(1001)</syntaxhighlight> |
||
==={{header|SmallBASIC}}=== |
|||
<syntaxhighlight lang="qbasic"> |
|||
While n^2 - (n-1)^2 < 1000 do n++ |
|||
print n |
|||
</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 433: | ||
PRINT fpow(1001) |
PRINT fpow(1001) |
||
END</syntaxhighlight> |
|||
END |
|||
</lang> |
|||
==={{Header|Tiny BASIC}}=== |
|||
<syntaxhighlight lang="qbasic"> LET N = 1001 |
|||
LET I = 0 |
|||
10 LET R = I*I - (I-1)*(I-1) |
|||
IF R >= N THEN GOTO 20 |
|||
IF R < N THEN LET I = I + 1 |
|||
GOTO 10 |
|||
20 PRINT I |
|||
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 192: | Line 455: | ||
print fpow(1001) |
print fpow(1001) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|bc}}== |
|||
<syntaxhighlight lang="bc">(1000 + 1) / 2 + 1</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 208: | Line 476: | ||
printf( "%d\n", f(1000) ); |
printf( "%d\n", f(1000) ); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
Line 214: | Line 482: | ||
=={{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 226: | Line 494: | ||
std::cout << answer.front() << '\n'; |
std::cout << answer.front() << '\n'; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 233: | Line 501: | ||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">import 'dart:math'; |
||
int leastSquare(int gap) { |
int leastSquare(int gap) { |
||
Line 245: | Line 513: | ||
void main() { |
void main() { |
||
print(leastSquare(1000)); |
print(leastSquare(1000)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>501</pre> |
<pre>501</pre> |
||
=={{header|dc}}== |
|||
<syntaxhighlight lang="dc">1000 1+2/1+p</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501</pre> |
|||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
procedure LeastSquareDiff(Memo: TMemo; Limit: integer); |
|||
var N: integer; |
|||
var S: string; |
|||
begin |
|||
for N:=1 to High(integer) do |
|||
if (N*N)-((N-1)*(N-1))>Limit then break; |
|||
S:=Format('Smallest Difference N^2: <%12d is: %12d',[Limit,N]); |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
procedure ShowLeastSquareDiff(Memo: TMemo); |
|||
begin |
|||
LeastSquareDiff(Memo,1000); |
|||
LeastSquareDiff(Memo,32000); |
|||
LeastSquareDiff(Memo,2000000000); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Smallest Difference N^2: < 1000 is: 501 |
|||
Smallest Difference N^2: < 32000 is: 16001 |
|||
Smallest Difference N^2: < 2000000000 is: 1000000001 |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="easylang"> |
|||
repeat |
|||
i += 1 |
|||
a = i * i |
|||
b = (i - 1) * (i - 1) |
|||
until a - b > 1000 |
|||
. |
|||
print i |
|||
</syntaxhighlight> |
|||
=={{header|EMal}}== |
|||
<syntaxhighlight lang="emal"> |
|||
fun leastSquare = int by int gap |
|||
for int n = 1; ; ++n |
|||
if n ** 2 - (n - 1) ** 2 > gap do return n end |
|||
end |
|||
end |
|||
writeLine(leastSquare(1000)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<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> |
||
501 |
501 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
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 271: | Line 604: | ||
=={{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|Forth}}== |
|||
{{works with|gforth|0.7.3}} |
|||
{{trans|Quackery}} |
|||
<syntaxhighlight lang="Forth">: square dup * ; |
|||
: square-diff |
|||
0 begin 1+ |
|||
dup square over 1- square - |
|||
1000 > until . ; |
|||
square-diff</syntaxhighlight> |
|||
{{out}}<pre>501 ok</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 287: | Line 634: | ||
end function |
end function |
||
print fpow(1001)</ |
print fpow(1001)</syntaxhighlight> |
||
{{out}}<pre>501</pre> |
{{out}}<pre>501</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn fpow( n as NSUInteger ) as NSUInteger |
|||
NSUInteger i = 0 |
|||
while ( i^2 - (i-1)^2 < n ) : i++ : wend |
|||
end fn = i |
|||
print fn fpow( 1001 ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
501 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 304: | Line 674: | ||
func main() { |
func main() { |
||
fmt.Println(squareDiff(1000)) |
fmt.Println(squareDiff(1000)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 313: | Line 683: | ||
=={{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 319: | Line 689: | ||
-- Or, with redundant verbosity |
-- Or, with redundant verbosity |
||
g n = |
g n = i |
||
where |
|||
let Just i = findIndex (> n) [1, 3..] |
|||
Just i = succ <$> findIndex (> n) [1, 3 ..] |
|||
in succ i |
|||
main = mapM_ print $ [f, g] <*> [1000]</syntaxhighlight> |
|||
main = do |
|||
print $ f 1000 |
|||
print $ g 1000</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>501 |
<pre>501 |
||
501</pre> |
501</pre> |
||
=={{header|J}}== |
|||
Through inspection, we can see that the "square difference' for a number <code>n</code> is <code>(2*n)-1</code>: |
|||
<syntaxhighlight lang=J> (*: - *:&<:) 1 2 3 4 5 |
|||
1 3 5 7 9</syntaxhighlight> |
|||
Therefore, we expect that n=501 would give us a "square difference' of 1001. Testing, we can see that this is the case: |
|||
<syntaxhighlight lang=J> (*: - *:&<:) 501 |
|||
1001</syntaxhighlight> |
|||
=={{header|Joy}}== |
|||
<syntaxhighlight lang="joy">1000 succ 2 / succ.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 337: | Line 720: | ||
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 348: | Line 731: | ||
=={{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|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let calculate x = |
|||
succ (succ x lsr 1) |
|||
let () = |
|||
Printf.printf "%u\n" (calculate 1000)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501</pre> |
|||
=={{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 378: | Line 770: | ||
=={{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 385: | Line 777: | ||
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 393: | Line 785: | ||
=={{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 404: | Line 796: | ||
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 416: | Line 808: | ||
</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 433: | Line 825: | ||
<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 449: | Line 841: | ||
<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|Phixmonti}}== |
|||
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Find_square_difference |
|||
by Galileo, 10/2022 #/ |
|||
def >1000 |
|||
dup dup * over 1 - dup * - 1001 < |
|||
enddef |
|||
0 true while 1 + >1000 endwhile print</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501 |
|||
=== Press any key to exit ===</pre> |
|||
=={{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 489: | Line 894: | ||
CALL PRINT$LEAST( 65000 ); |
CALL PRINT$LEAST( 65000 ); |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 496: | Line 901: | ||
THE LOWEST N WHERE THE SQUARES OF N AND N-1 DIFFER BY MORE THAN 65000 IS: 32501 |
THE LOWEST N WHERE THE SQUARES OF N AND N-1 DIFFER BY MORE THAN 65000 IS: 32501 |
||
</pre> |
</pre> |
||
=={{header|Plain English}}== |
|||
{{libheader|Plain English-output}} |
|||
<syntaxhighlight lang="text"> |
|||
To run: |
|||
Start up. |
|||
Put 1 into a counter. |
|||
Loop. |
|||
If the counter does have a square difference, write the counter to the output; break. |
|||
Bump the counter. |
|||
Repeat. |
|||
Wait for the escape key. |
|||
Shut down. |
|||
To decide if a number does have a square difference: |
|||
Put the number into another number. |
|||
Raise the other number to 2. |
|||
Put the number into a third number. |
|||
Subtract 1 from the third number. |
|||
Raise the third number to 2. |
|||
If the other number minus the third number is greater than 1000, say yes. |
|||
Say no. |
|||
</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
import math |
import math |
||
print("working...") |
print("working...") |
||
Line 516: | Line 944: | ||
print("done...") |
print("done...") |
||
print() |
print() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 525: | Line 953: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
<syntaxhighlight lang="quackery"> [ dup * ] is squared ( n --> n ) |
|||
<lang Quackery> [ dup * ] is squared ( n --> n ) |
|||
0 |
0 |
||
Line 533: | Line 960: | ||
over 1 - squared - |
over 1 - squared - |
||
1000 > until ] |
1000 > until ] |
||
echo</ |
echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 540: | Line 967: | ||
==={{header|Using algebra}}=== |
==={{header|Using algebra}}=== |
||
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 550: | Line 976: | ||
=={{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 573: | Line 999: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 581: | Line 1,007: | ||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
≪ 1 1 |
|||
'''DO''' |
|||
SWAP 1 + DUP SQ |
|||
'''UNTIL''' DUP 4 ROLL - 1000 > '''END''' DROP |
|||
≫ EVAL |
|||
{{out}} |
|||
<pre> |
|||
1: 501 |
|||
</pre> |
|||
The above program takes 16.4 seconds to be executed on a HP-28S. |
|||
===Algebraic approach=== |
|||
≪ 'n^2-(n-1)^2-1000' |
|||
EXPAN COLCT 'n' ISOL CEIL |
|||
≫ EVAL |
|||
which returns the same result in only 9.2 seconds. |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby">p (1..).detect{|n| n*n - (n-1)*(n-1) > 1000 }</syntaxhighlight> |
|||
{{out}} |
|||
<pre>501 |
|||
</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var N = 1000 |
||
# Binary search |
# Binary search |
||
Line 595: | Line 1,043: | ||
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 602: | Line 1,050: | ||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
< |
<syntaxhighlight lang="verilog">module main; |
||
integer i, n; |
integer i, n; |
||
Line 612: | Line 1,060: | ||
$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="wren">var squareDiff = Fn.new { |k| ((k + 1) * 0.5).ceil } |
||
System.print(squareDiff.call(1000))</ |
System.print(squareDiff.call(1000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 632: | Line 1,080: | ||
n > 500.5 |
n > 500.5 |
||
n = 501 |
n = 501 |
||
</pre> |
|||
=={{header|zig}}== |
|||
<syntaxhighlight lang="zig"> |
|||
const std = @import("std"); |
|||
const print = @import("std").debug.print; |
|||
pub fn main() !void { |
|||
var number: u64 = 2; |
|||
while(true) { |
|||
const sq= number * number; |
|||
const number1= number - 1; |
|||
const sq1= number1 * number1; |
|||
if (sq - sq1 > 1000 ) { |
|||
print("Result= {}\n", .{ number }); |
|||
break; |
|||
} |
|||
number += 1; |
|||
if (number > 10000) { |
|||
print("No find ! \n",.{}); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Result= 501 |
|||
</pre> |
</pre> |
||
[[Category:Simple]] |
[[Category:Simple]] |
Latest revision as of 10:35, 26 June 2024
- Task
Find and show on this page the least positive integer number n, where difference of n*n and (n-1)*(n-1) greater than 1000.
The result is 501 because 501*501 - 500*500 = 251001 - 250000 = 1001 > 1000.
11l
L(n) 1..
I n^2 - (n - 1)^2 > 1000
print(n)
L.break
- Output:
501
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program diffsquare64.s */
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ MAXI, 10000
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessNotFind: .asciz "No soluce !! "
szMessResult: .asciz "Nombre = "
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
mov x5,#2 // init start number
ldr x3,iMAXI
1: // begin loop
mul x2,x5,x5 // n * n
sub x4,x5,#1 // n - 1
mul x6,x4,x4 // (n - 1) * (n - 1)
sub x0,x2,x6 // difference
cmp x0,#1000 // > 1000
bgt 2f
add x5,x5,#1 // increment number
cmp x5,x3 // maxi limit ?
blt 1b
ldr x0,qAdrszMessNotFind // display not find
bl affichageMess
b 100f
2: // display result
mov x0,x5
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
mov x0,#3 // number string to display
ldr x1,qAdrszMessResult
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszMessNotFind: .quad szMessNotFind
qAdrszMessStart: .quad szMessStart
iMAXI: .quad MAXI
/***************************************************/
/* display multi strings */
/* new version 24/05/2023 */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* x4 address string4 */
/* x5 address string5 */
/* x6 address string5 */
displayStrings: // INFO: displayStrings
stp x7,lr,[sp,-16]! // save registers
stp x2,fp,[sp,-16]! // save registers
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes)
mov x7,x0 // save strings number
cmp x7,#0 // 0 string -> end
ble 100f
mov x0,x1 // string 1
bl affichageMess
cmp x7,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x7,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x7,#3
ble 100f
mov x0,x4
bl affichageMess
cmp x7,#4
ble 100f
mov x0,x5
bl affichageMess
cmp x7,#5
ble 100f
mov x0,x6
bl affichageMess
100:
ldp x2,fp,[sp],16 // restaur registers
ldp x7,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
- Output:
Program 64 bits start. Nombre = 501
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure Find_Square_Difference is
Last : Natural := 0;
Square : Positive;
Diff : Positive;
begin
for N in 1 .. Positive'Last loop
Square := N ** 2;
Diff := Square - Last;
Last := Square;
if Diff > 1000 then
Put_Line (N'Image);
exit;
end if;
end loop;
end Find_Square_Difference;
- Output:
501
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.
BEGIN # find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 #
# shows the smalled square n where n^2 - (n-1)^2 is greater than required difference #
# n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 #
# so 2n - 1 > required difference or n > ( required difference + 1 ) / 2 #
PROC show least square = ( INT required difference )VOID:
print( ( "Smallest n where n^2 - (n-1)^2 is > ", whole( required difference, -12 )
, " is: ", whole( ( ( required difference + 1 ) OVER 2 ) + 1, -12 )
, newline
)
);
show least square( 1 000 );
show least square( 32 000 );
show least square( 2 000 000 000 )
END
- Output:
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501 Smallest n where n^2 - (n-1)^2 is > 32000 is: 16001 Smallest n where n^2 - (n-1)^2 is > 2000000000 is: 1000000001
ALGOL W
begin % find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 %
integer requiredDifference;
requiredDifference := 1000;
write( i_w := 1, s_w := 0,
, "Smallest n where n^2 - (n-1)^2 is > ", requiredDifference
, " is: ", ( ( requiredDifference + 1 ) div 2 ) + 1
)
end.
- Output:
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501
ARM Assembly
/* ARM assembly Raspberry PI */
/* program diffsquare.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ MAXI, 10000
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessNotFind: .asciz "No soluce !! "
szMessResult: .asciz "Nombre = "
szMessStart: .asciz "Program 32 bits start.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
mov r5,#2 @ init start number
ldr r3,iMAXI
1: @ begin loop
mul r2,r5,r5 @ n * n
sub r4,r5,#1 @ n - 1
mul r6,r4,r4 @ (n - 1) * (n - 1)
sub r0,r2,r6 @ difference
cmp r0,#1000 @ > 1000
bgt 2f
add r5,r5,#1 @ increment number
cmp r5,r3 @ maxi limit ?
blt 1b
ldr r0,iAdrszMessNotFind @ display not find
bl affichageMess
b 100f
2: @ display result
mov r0,r5
ldr r1,iAdrsZoneConv
bl conversion10 @ decimal conversion
mov r3,#0 @ zero final
strb r3,[r1,r0]
mov r0,#3 @ number string to display
ldr r1,iAdrszMessResult
ldr r2,iAdrsZoneConv @ insert conversion in message
ldr r3,iAdrszCarriageReturn
bl displayStrings @ display message
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsZoneConv: .int sZoneConv
iAdrszMessResult: .int szMessResult
iAdrszMessNotFind: .int szMessNotFind
iAdrszMessStart: .int szMessStart
iMAXI: .int MAXI
/***************************************************/
/* display multi strings */
/***************************************************/
/* r0 contains number strings address */
/* r1 address string1 */
/* r2 address string2 */
/* r3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: @ INFO: displayStrings
push {r1-r4,fp,lr} @ save des registres
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes)
mov r4,r0 @ save strings number
cmp r4,#0 @ 0 string -> end
ble 100f
mov r0,r1 @ string 1
bl affichageMess
cmp r4,#1 @ number > 1
ble 100f
mov r0,r2
bl affichageMess
cmp r4,#2
ble 100f
mov r0,r3
bl affichageMess
cmp r4,#3
ble 100f
mov r3,#3
sub r2,r4,#4
1: @ loop extract address string on stack
ldr r0,[fp,r2,lsl #2]
bl affichageMess
subs r2,#1
bge 1b
100:
pop {r1-r4,fp,pc}
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
- Output:
Program 32 bits start. Nombre = 501
Arturo
i: new 1
while ø [
if 1000 < (i^2)-(dec i)^2
-> break
inc 'i
]
print i
- Output:
501
Asymptote
int fpow(int n) {
int i = 0;
while (i^2 - (i-1)^2 < n) ++i;
return i;
}
write(fpow(1000));
AutoHotkey
while ((n:=A_Index)**2 - (n-1)**2 < 1000)
continue
MsgBox % result := n
- Output:
501
AWK
# syntax: GAWK -f FIND_SQUARE_DIFFERENCE.AWK
BEGIN {
n = 1001
while (i^2-(i-1)^2 < n) {
i++
}
printf("%d\n",i)
exit(0)
}
- Output:
501
BASIC
BASIC256
function fpow(n)
i = 0
while i^2 - (i-1)^2 < n
i += 1
end while
Return i
end function
print fpow(1001)
end
PureBasic
Procedure fpow(n.i)
Define i.i
While Pow(i, 2) - Pow((i-1), 2) < n
i + 1
Wend
ProcedureReturn i
EndProcedure
OpenConsole()
Print(Str(fpow(1001)))
Input()
CloseConsole()
QBasic
FUNCTION fpow (n)
WHILE (i * i) - ((i - 1) * (i - 1)) < n
i = i + 1
WEND
fpow = i
END FUNCTION
PRINT fpow(1001)
Run BASIC
function fpow(n)
while i^2-(i-1)^2 < n
i = i+1
wend
fpow = i
end function
print fpow(1001)
SmallBASIC
While n^2 - (n-1)^2 < 1000 do n++
print n
True BASIC
FUNCTION fpow (n)
DO WHILE i ^ 2 - (i - 1) ^ 2 < n
LET i = i + 1
LOOP
LET fpow = I
END FUNCTION
PRINT fpow(1001)
END
Tiny BASIC
LET N = 1001
LET I = 0
10 LET R = I*I - (I-1)*(I-1)
IF R >= N THEN GOTO 20
IF R < N THEN LET I = I + 1
GOTO 10
20 PRINT I
END
Yabasic
sub fpow(n)
while i^2 - (i-1)^2 < n
i = i + 1
wend
Return i
end sub
print fpow(1001)
end
bc
(1000 + 1) / 2 + 1
- Output:
501
C
#include<stdio.h>
#include<stdlib.h>
int f(int n) {
int i, i1;
for(i=1;i*i-i1*i1<n;i1=i, i++);
return i;
}
int main(void) {
printf( "%d\n", f(1000) );
return 0;
}
- Output:
501
C++
The C solution is also idomatic in C++. An alterate approach is to use Ranges from C++20.
#include <iostream>
#include <ranges>
int main()
{
const int maxSquareDiff = 1000;
auto squareCheck = [maxSquareDiff](int i){return 2 * i - 1 > maxSquareDiff;};
auto answer = std::views::iota(1) | // {1, 2, 3, 4, 5, ....}
std::views::filter(squareCheck) | // filter out the ones that are below 1000
std::views::take(1); // take the first one
std::cout << answer.front() << '\n';
}
- Output:
501
Dart
import 'dart:math';
int leastSquare(int gap) {
for (int n = 1;; n++) {
if (pow(n, 2) - pow((n - 1), 2) > gap) {
return n;
}
}
}
void main() {
print(leastSquare(1000));
}
- Output:
501
dc
1000 1+2/1+p
- Output:
501
Delphi
procedure LeastSquareDiff(Memo: TMemo; Limit: integer);
var N: integer;
var S: string;
begin
for N:=1 to High(integer) do
if (N*N)-((N-1)*(N-1))>Limit then break;
S:=Format('Smallest Difference N^2: <%12d is: %12d',[Limit,N]);
Memo.Lines.Add(S);
end;
procedure ShowLeastSquareDiff(Memo: TMemo);
begin
LeastSquareDiff(Memo,1000);
LeastSquareDiff(Memo,32000);
LeastSquareDiff(Memo,2000000000);
end;
- Output:
Smallest Difference N^2: < 1000 is: 501 Smallest Difference N^2: < 32000 is: 16001 Smallest Difference N^2: < 2000000000 is: 1000000001
EasyLang
repeat
i += 1
a = i * i
b = (i - 1) * (i - 1)
until a - b > 1000
.
print i
EMal
fun leastSquare = int by int gap
for int n = 1; ; ++n
if n ** 2 - (n - 1) ** 2 > gap do return n end
end
end
writeLine(leastSquare(1000))
- Output:
501
F#
let n=1000 in printfn $"%d{((n+1)/2)+1}"
- Output:
501
Factor
The difference between squares is the odd numbers, so ls(n)=⌈n/2+1⌉.
USING: math math.functions prettyprint ;
: least-sq ( m -- n ) 2 / 1 + ceiling ;
1000 least-sq .
- Output:
501
Fermat
Func F(n) =
i:=0;
while i^2-(i-1)^2<n do i:=i+1 od; i.;
!!F(1000);
- Output:
501
Forth
: square dup * ;
: square-diff
0 begin 1+
dup square over 1- square -
1000 > until . ;
square-diff
- Output:
501 ok
FreeBASIC
function fpow(n as uinteger) as uinteger
dim as uinteger i
while i^2-(i-1)^2 < n
i+=1
wend
return i
end function
print fpow(1001)
- Output:
501
FutureBasic
local fn fpow( n as NSUInteger ) as NSUInteger
NSUInteger i = 0
while ( i^2 - (i-1)^2 < n ) : i++ : wend
end fn = i
print fn fpow( 1001 )
HandleEvents
- Output:
501
Go
package main
import (
"fmt"
"math"
)
func squareDiff(k int) int {
return int(math.Ceil(float64(k+1) * 0.5))
}
func main() {
fmt.Println(squareDiff(1000))
}
- Output:
501
Haskell
The sequence of differences between successive squares is the sequence of odd numbers.
import Data.List (findIndex)
f = succ . flip div 2
-- Or, with redundant verbosity
g n = i
where
Just i = succ <$> findIndex (> n) [1, 3 ..]
main = mapM_ print $ [f, g] <*> [1000]
- Output:
501 501
J
Through inspection, we can see that the "square difference' for a number n
is (2*n)-1
:
(*: - *:&<:) 1 2 3 4 5
1 3 5 7 9
Therefore, we expect that n=501 would give us a "square difference' of 1001. Testing, we can see that this is the case:
(*: - *:&<:) 501
1001
Joy
1000 succ 2 / succ.
- Output:
501
jq
Works with gojq, the Go implementation of jq
So this question is essentially asking to solve `2n - 1 > 1000`. Wow.
At the risk of hastening RC's demise, one could offer a jq solution like so:
first( range(1; infinite) | select( 2 * . - 1 > 1000 ) )
Or, for anyone envious of Bitcoin's contribution to global warming:
first( range(1; infinite) | select( .*. - ((.-1) | .*.) > 1000 ) )
- Output:
501
Julia
julia> findfirst(n -> n*n - (n-1)*(n-1) > 1000, 1:1_000_000)
501
OCaml
let calculate x =
succ (succ x lsr 1)
let () =
Printf.printf "%u\n" (calculate 1000)
- Output:
501
Pari/GP
F(n)=i=0;while(i^2-(i-1)^2<n,i=i+1);return(i);
print(F(1000))
- Output:
501
Pencil and Paper
Find the smallest positive integer number n, where the difference of n2 and (n - 1)2 is greater than 1000.
r: roots of squares s: successive squares d: differences between successive squares, (a.k.a, the list of positive odd integers) r: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... s: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ... d: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, ... r: n s: n * n d: n * 2 + 1 solve for d > 1,000 the first odd integer greater than 1,000 is 1,001 (1,001 + 1) / 2 = 501 ( = n)
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Least_square
use warnings;
my $n = 1;
$n++ until $n ** 2 - ($n-1) ** 2 > 1000;
print "$n\n";
- Output:
501
Phix
Essentially Wren equivalent, but explained in excruciating detail especially for enyone that evidently needs elp, said Eeyore.
with javascript_semantics printf(1,""" n*n - (n - 1)*(n - 1) > 1000 n*n - (n*n - 2*n + 1) > 1000 n*n - n*n + 2*n - 1 > 1000 2*n - 1 > 1000 2*n > 1001 n > 500.5 n = %d """,ceil(500.5))
- Output:
n*n - (n - 1)*(n - 1) > 1000 n*n - (n*n - 2*n + 1) > 1000 n*n - n*n + 2*n - 1 > 1000 2*n - 1 > 1000 2*n > 1001 n > 500.5 n = 501
Or if you prefer, same output:
with javascript_semantics string e -- equation procedure p(string s) e := s -- set/save printf(1,"%s\n",s) end procedure p("n*n - (n - 1)*(n - 1) > 1000") -- original p(substitute(e,"(n - 1)*(n - 1)", "(n*n - 2*n + 1)")) -- expand string{l,m,r} = scanf(e,"%s - (%s)%s")[1] m = substitute_all(m,"-+|","|-+") -- unsign p(sprintf("%s - %s%s",{l,m,r})) p(substitute(e,"n*n - n*n + ","")) -- eliminate p(substitute_all(e,{" - 1","1000"},{"","1001"})) -- add 1 p(substitute_all(e,{"2*","1001"},{"","500.5"})) -- divide by 2 p(substitute(e,"> 500.5",sprintf("= %d",ceil(500.5)))) -- solve
or even:
without js -- (user defined types are not implicitly called) type pstring(string s) printf(1,"%s\n",s) return true end type pstring e -- equation e = "n*n - (n - 1)*(n - 1) > 1000" -- original e = substitute(e,"(n - 1)*(n - 1)", "(n*n - 2*n + 1)") -- expand string{l,m,r} = scanf(e,"%s - (%s)%s")[1] m = substitute_all(m,"-+|","|-+") -- unsign e = sprintf("%s - %s%s",{l,m,r}) e = substitute(e,"n*n - n*n + ","") -- eliminate e = substitute_all(e,{" - 1","1000"},{"","1001"}) -- add 1 e = substitute_all(e,{"2*","1001"},{"","500.5"}) -- divide by 2 e = substitute(e,"> 500.5",sprintf("= %d",ceil(500.5))) -- solve
Phixmonti
/# Rosetta Code problem: http://rosettacode.org/wiki/Find_square_difference
by Galileo, 10/2022 #/
def >1000
dup dup * over 1 - dup * - 1001 <
enddef
0 true while 1 + >1000 endwhile print
- Output:
501 === Press any key to exit ===
PL/M
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator.
Note that the original 8080 PL/M compiler only supports 8 and 16 bit unisgned numbers.
100H: /* FIND THE LEAST +VE N WHERE N SQUARED - (N-1) SQUARED > 1000 */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
GOTO 5;
END BDOS;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
PRINT$LEAST: PROCEDURE( DIFF );
DECLARE DIFF ADDRESS;
CALL PR$STRING( . 'THE LOWEST N WHERE THE SQUARES OF N AND N-1 $' );
CALL PR$STRING( . 'DIFFER BY MORE THAN $' );
CALL PR$NUMBER( DIFF );
CALL PR$STRING( .' IS: $' );
CALL PR$NUMBER( ( ( DIFF + 1 ) / 2 ) + 1 );
CALL PR$NL;
END PRINT$LEAST ;
CALL PRINT$LEAST( 1000 );
CALL PRINT$LEAST( 32000 );
CALL PRINT$LEAST( 65000 );
EOF
- Output:
THE LOWEST N WHERE THE SQUARES OF N AND N-1 DIFFER BY MORE THAN 1000 IS: 501 THE LOWEST N WHERE THE SQUARES OF N AND N-1 DIFFER BY MORE THAN 32000 IS: 16001 THE LOWEST N WHERE THE SQUARES OF N AND N-1 DIFFER BY MORE THAN 65000 IS: 32501
Plain English
To run:
Start up.
Put 1 into a counter.
Loop.
If the counter does have a square difference, write the counter to the output; break.
Bump the counter.
Repeat.
Wait for the escape key.
Shut down.
To decide if a number does have a square difference:
Put the number into another number.
Raise the other number to 2.
Put the number into a third number.
Subtract 1 from the third number.
Raise the third number to 2.
If the other number minus the third number is greater than 1000, say yes.
Say no.
Python
import math
print("working...")
limit1 = 6000
limit2 = 1000
oldSquare = 0
newSquare = 0
for n in range(limit1):
newSquare = n*n
if (newSquare - oldSquare > limit2):
print("Least number is = ", end = "");
print(int(math.sqrt(newSquare)))
break
oldSquare = n*n
print("done...")
print()
- Output:
working... Least number is = 501 done...
Quackery
[ dup * ] is squared ( n --> n )
0
[ 1+
dup squared
over 1 - squared -
1000 > until ]
echo
- Output:
501
Using algebra
Noting that a²-b² ≡ (a+b)(a-b), and that in this instance a = b+1.
1000 2 / 1+ echo
- Output:
501
Raku
say first { $_² - ($_-1)² > 1000 }, ^Inf;
- Output:
501
Ring
load "stdlib.ring"
see "working..." + nl
limit1 = 6000
limit2 = 1000
oldPrime = 0
newPrime = 0
for n = 1 to limit1
newPrime = n*n
if newPrime - oldPrime > limit2
see "Latest number is = " + sqrt(newPrime) + nl
exit
ok
oldPrime = n*n
next
see "done..." + nl
- Output:
working... Latest number is = 501 done...
RPL
≪ 1 1 DO SWAP 1 + DUP SQ UNTIL DUP 4 ROLL - 1000 > END DROP ≫ EVAL
- Output:
1: 501
The above program takes 16.4 seconds to be executed on a HP-28S.
Algebraic approach
≪ 'n^2-(n-1)^2-1000' EXPAN COLCT 'n' ISOL CEIL ≫ EVAL
which returns the same result in only 9.2 seconds.
Ruby
p (1..).detect{|n| n*n - (n-1)*(n-1) > 1000 }
- Output:
501
Sidef
var N = 1000
# Binary search
var n = bsearch_ge(1, N, {|k|
k**2 - (k-1)**2 <=> N+1
})
# Closed-form
var m = ceil((N + 1 + N&1) / 2)
assert(n**2 - (n-1)**2 > N)
assert_eq(n, m)
say "#{n}^2 - #{n-1}^2 = #{n**2 - (n-1)**2}"
- Output:
501^2 - 500^2 = 1001
Verilog
module main;
integer i, n;
initial begin
n = 1000;
i = 0;
while (i ** 2 - (i - 1) ** 2 < n) i=i+1;
$display(i);
$finish ;
end
endmodule
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.
var squareDiff = Fn.new { |k| ((k + 1) * 0.5).ceil }
System.print(squareDiff.call(1000))
- Output:
501
XPL0
n^2 - (n - 1)^2 > 1000 n^2 - (n^2 - 2n + 1) > 1000 2n - 1 > 1000 2n > 1001 n > 500.5 n = 501
zig
const std = @import("std");
const print = @import("std").debug.print;
pub fn main() !void {
var number: u64 = 2;
while(true) {
const sq= number * number;
const number1= number - 1;
const sq1= number1 * number1;
if (sq - sq1 > 1000 ) {
print("Result= {}\n", .{ number });
break;
}
number += 1;
if (number > 10000) {
print("No find ! \n",.{});
break;
}
}
}
- Output:
Result= 501
- Draft Programming Tasks
- 11l
- AArch64 Assembly
- Ada
- ALGOL 68
- ALGOL W
- ARM Assembly
- Arturo
- Asymptote
- AutoHotkey
- AWK
- BASIC
- BASIC256
- PureBasic
- QBasic
- Run BASIC
- SmallBASIC
- True BASIC
- Tiny BASIC
- Yabasic
- Bc
- C
- C++
- Dart
- Dc
- Delphi
- SysUtils,StdCtrls
- EasyLang
- EMal
- F Sharp
- Factor
- Fermat
- Forth
- FreeBASIC
- FutureBasic
- Go
- Haskell
- J
- Joy
- Jq
- Julia
- OCaml
- Pari/GP
- Pencil and Paper
- Perl
- Phix
- Phixmonti
- PL/M
- Plain English
- Plain English-output
- Python
- Quackery
- Using algebra
- Raku
- Ring
- RPL
- Ruby
- Sidef
- Verilog
- Wren
- XPL0
- Zig
- Simple