Find square difference: Difference between revisions
m (→{{header|PL/M}}: Added syntax highlighting) |
(Added XPL0 example.) |
||
Line 261: | Line 261: | ||
<pre> |
<pre> |
||
501 |
501 |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<pre> |
|||
n^2 - (n - 1)^2 > 1000 |
|||
n^2 - (n^2 - 2n + 1) > 1000 |
|||
2n - 1 > 1000 |
|||
2n > 1001 |
|||
n > 500.5 |
|||
n = 501 |
|||
</pre> |
</pre> |
Revision as of 19:39, 19 November 2021
- 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.
ALGOL 68
Using the same school maths ( or math for those in the US ) in the Wren version but using a calculation.
Also showng the least positive integer where the difference between n^2 and (n-1)^2 is greater than 32 000 and 2 000 000 000 - using only 32 bit integers.
<lang algol68>BEGIN # find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 #
INT required diff = 1 000; INT medium diff = 32 000; INT larger diff = 2 000 000 000; # return s space-separated into groups of 3 digits # PROC space separate = ( STRING unformatted )STRING: BEGIN STRING result := ""; INT ch count := 0; FOR c FROM UPB unformatted BY -1 TO LWB unformatted DO IF ch count <= 2 THEN ch count +:= 1 ELSE ch count := 1; " " +=: result FI; unformatted[ c ] +=: result OD; result END # space separate # ; # n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 # # so 2n - 1 > 1000 or n > 1001/2 # print( ( "Smallest n where n^2 - (n-1)^2 is > ", space separate( whole( required diff, -12 ) ), ": " , space separate( whole( ( ( required diff + 1 ) OVER 2 ) + 1, -12 ) ) , newline ) ); print( ( "Smallest n where n^2 - (n-1)^2 is > ", space separate( whole( medium diff, -12 ) ), ": " , space separate( whole( ( ( medium diff + 1 ) OVER 2 ) + 1, -12 ) ) , newline ) ); print( ( "Smallest n where n^2 - (n-1)^2 is > ", space separate( whole( larger diff, -12 ) ), ": " , space separate( whole( ( ( larger diff + 1 ) OVER 2 ) + 1, -12 ) ) , newline ) )
END</lang>
- Output:
Smallest n where n^2 - (n-1)^2 is > 1 000: 501 Smallest n where n^2 - (n-1)^2 is > 32 000: 16 001 Smallest n where n^2 - (n-1)^2 is > 2 000 000 000: 1 000 000 001
C
<lang 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;
}</lang>
- Output:
501
Dart
<lang 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));
}</lang>
- Output:
501
Fermat
<lang>Func F(n) =
i:=0; while i^2-(i-1)^2<n do i:=i+1 od; i.;
!!F(1000);</lang>
- Output:
501
FreeBASIC
<lang 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)</lang>
- Output:
501
Pari/GP
<lang parigp>F(n)=i=0;while(i^2-(i-1)^2<n,i=i+1);return(i); print(F(1000))</lang>
- Output:
501
Perl
<lang 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";</lang>
- Output:
501
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.
<lang pli>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</lang>
- 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
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
Python
<lang 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() </lang>
- Output:
working... Least number is = 501 done...
Raku
<lang perl6>say first { $_² - ($_-1)² > 1000 }, ^Inf;</lang>
- Output:
501
Ring
<lang 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 </lang>
- Output:
working... Latest number is = 501 done...
Wren
n needs or be such that n² - (n² - 2n + 1) > 1000 or n > 500.5. <lang ecmascript>System.print(500.5.ceil)</lang>
- 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