How to resolve the algorithm Sequence of non-squares step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sequence of non-squares step by step in the Seed7 programming language

Table of Contents

Problem Statement

Show that the following remarkable formula gives the sequence of non-square natural numbers:

This is sequence   A000037   in the OEIS database.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sequence of non-squares step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";
  include "float.s7i";
  include "math.s7i";
 
const func integer: nonsqr (in integer: n) is
  return n + trunc(0.5 + sqrt(flt(n)));
 
const proc: main is func
  local
    var integer: i is 0;
    var float: j is 0.0;
  begin
    # First 22 values (as a list) has no squares:
    for i range 1 to 22 do
      write(nonsqr(i) <& " ");
    end for;
    writeln;
 
    # The following check shows no squares up to one million:
    for i range 1 to 1000000 do
      j := sqrt(flt(nonsqr(i)));
      if j = floor(j) then
        writeln("Found square for nonsqr(" <& i <& ")");
      end if;
    end for;
  end func;

  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the Sidef programming language
You may also check:How to resolve the algorithm Metaprogramming step by step in the C programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the ALGOL 68 programming language