Find square difference: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Dart}}: Added Dart solution)
Line 18: Line 18:
n is 501
n is 501
</pre>
</pre>

=={{header|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>
{{out}}
<pre>501</pre>


=={{header|Perl}}==
=={{header|Perl}}==

Revision as of 07:08, 19 November 2021

Find square difference is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Find and show on this page the least positive integer number n, where diffrence 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... <lang algol68>BEGIN # find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 #

   INT rqd diff = 1000;
   # 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( ( "n is ", whole( ( ( rqd diff + 1 ) OVER 2 ) + 1, 0 ) ) )

END</lang>

Output:
n is 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

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

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