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

Published on 12 May 2024 09:40 PM
#Go

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

The provided Go code performs two distinct tasks related to the concept of "remarkable" numbers and their relationship with square numbers.

Task 1: Generating Remarkable Numbers

  • The remarkable() function takes an integer n as input.
  • It returns a new integer that is calculated as n plus the square root of n rounded up to the nearest integer.
  • This function essentially generates "remarkable" numbers, which are numbers that lie between n and n + √n.
  • The main function prints a table showing the relationship between n and its corresponding remarkable number r(n) for values of n from 1 to 22.

Task 2: Checking for Squares Among Remarkable Numbers

  • The main function sets a constant limit with a value of 1e6, which represents the upper limit for checking.
  • It initializes two variables, next and nextSq, which will be used to track the next expected perfect square number. Initially, next is set to 2, and nextSq is set to 4 (the square of 2).
  • The main function iterates over integers n from 1 to the specified limit (1e6).
  • For each n, it calculates the remarkable number r based on the remarkable() function.
  • The code then checks the relationship between r and the current expected square number nextSq:
    • If r is equal to nextSq, it means that a square number has occurred among the remarkable numbers. This is an unexpected and unusual occurrence, as remarkable numbers are typically not perfect squares. The code panics, indicating that a square number has been found.
    • If r is greater than nextSq, it means that the expected square number has not yet occurred. The code prints a message indicating that nextSq did not occur. It then increments next and recalculates nextSq as the square of the new next.
  • The iteration continues until all integers n from 1 to the limit have been checked.
  • If no square numbers are found among the remarkable numbers within the specified limit, the code prints a message indicating that "No squares occur for n <" the limit.

In summary, the code first generates remarkable numbers for a range of input values and then checks whether any of these remarkable numbers are square numbers. If a square number is found among the remarkable numbers, the code raises a panic, indicating an unusual occurrence. If no square numbers are found within the specified limit, the code reports that no squares occurred.

Source code in the go programming language

package main

import (
    "fmt"
    "math"
)

func remarkable(n int) int {
    return n + int(.5+math.Sqrt(float64(n)))
}

func main() {
    // task 1
    fmt.Println("  n  r(n)")
    fmt.Println("---  ---")
    for n := 1; n <= 22; n++ {
        fmt.Printf("%3d  %3d\n", n, remarkable(n))
    }

    // task 2
    const limit = 1e6
    fmt.Println("\nChecking for squares for n <", limit)
    next := 2
    nextSq := 4
    for n := 1; n < limit; n++ {
        r := remarkable(n)
        switch {
        case r == nextSq:
            panic(n)
        case r > nextSq:
            fmt.Println(nextSq, "didn't occur")
            next++
            nextSq = next * next
        }
    }
    fmt.Println("No squares occur for n <", limit)
}


  

You may also check:How to resolve the algorithm Strip block comments step by step in the Wren programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Delphi programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Phix programming language
You may also check:How to resolve the algorithm Combinations step by step in the OCaml programming language
You may also check:How to resolve the algorithm String prepend step by step in the Standard ML programming language