Find square difference: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Forth entry)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 9 users not shown)
Line 16:
501
</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}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
Line 43 ⟶ 167:
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
# INT so 2n - 1 > required difference =or n > ( required difference + 1 ) / 2 test[ i ];#
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 > required difference or n > ( required difference + 1 ) / 2 #
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 );
OD
show least square( 32 000 );
END</syntaxhighlight>
show least square( 2 000 000 000 )
END
</syntaxhighlight>
{{out}}
<pre>
Line 74 ⟶ 200:
<pre>
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501
</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
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"
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
Nombre = 501
</pre>
 
Line 202 ⟶ 451:
end</syntaxhighlight>
 
 
=={{header|bc}}==
<syntaxhighlight lang="bc">(1000 + 1) / 2 + 1</syntaxhighlight>
{{out}}
<pre>501</pre>
 
=={{header|C}}==
Line 256 ⟶ 510:
{{out}}
<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}}==
Line 261 ⟶ 553:
repeat
i += 1
squarea = pow i 2* i
diffSquareb = pow (i - 1) 2* (i - 1)
until a - b > 1000
difference = square - diffSquare
until difference > 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#}}==
Line 277 ⟶ 582:
501
</pre>
 
 
=={{header|Factor}}==
Line 324 ⟶ 630:
print fpow(1001)</syntaxhighlight>
{{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}}==
Line 372 ⟶ 701:
<syntaxhighlight lang=J> (*: - *:&<:) 501
1001</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">1000 succ 2 / succ.</syntaxhighlight>
{{out}}
<pre>501</pre>
 
=={{header|jq}}==
Line 667 ⟶ 1,001:
</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}}==
<syntaxhighlight lang="ruby">var N = 1000
Line 702 ⟶ 1,058:
=={{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''.
<syntaxhighlight lang="ecmascriptwren">var squareDiff = Fn.new { |k| ((k + 1) * 0.5).ceil }
System.print(squareDiff.call(1000))</syntaxhighlight>
 
Line 718 ⟶ 1,074:
n > 500.5
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>
[[Category:Simple]]
9,476

edits