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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sequence of non-squares step by step in the CoffeeScript 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 CoffeeScript programming language

Source code in the coffeescript programming language

non_square = (n) -> n + Math.floor(1/2 + Math.sqrt(n))

is_square = (n) ->
  r = Math.floor(Math.sqrt(n))
  r * r is n

do ->
  first_22_non_squares = (non_square i for i in [1..22])
  console.log first_22_non_squares
  
  # test is_square has no false negatives:
  for i in [1..10000]
    throw Error("is_square broken") unless is_square i*i
    
  # test non_square is valid for first million values of n
  for i in [1..1000000]
    throw Error("non_square broken") if is_square non_square(i)

  console.log "success"


  

You may also check:How to resolve the algorithm Loops/Do-while step by step in the AWK programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the AWK programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the BASIC programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Oforth programming language