How to resolve the algorithm Consecutive primes with ascending or descending differences step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Consecutive primes with ascending or descending differences step by step in the Go programming language

Table of Contents

Problem Statement

Find and display here on this page, the longest sequence of consecutive prime numbers where the differences between the primes are strictly ascending. Do the same for sequences of primes where the differences are strictly descending.

In both cases, show the sequence for primes   <   1,000,000.

If there are multiple sequences of the same length, only the first need be shown.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Consecutive primes with ascending or descending differences step by step in the Go programming language

This Go program finds the longest sequences of prime numbers with either strictly increasing or decreasing differences between consecutive primes.

  • package main declares the main package.

  • import imports the necessary package (rcu which contains a function for generating prime numbers up to a specified limit).

  • const LIMIT = 999999 sets the limit for generating primes.

  • var primes = rcu.Primes(LIMIT) generates all the prime numbers up to the specified limit and stores them in the primes slice.

  • func longestSeq(dir string) finds the longest sequences of prime numbers with either strictly increasing or decreasing differences between consecutive primes, depending on the direction specified by the dir parameter.

    • It initializes variables (pd, longSeqs, currSeq) for keeping track of the longest sequences.
    • It iterates through the primes slice.
    • For each prime, it calculates the difference from the previous prime and checks if this difference matches the desired direction.
    • If the direction matches, it updates currSeq and longSeqs accordingly.
    • After iterating through all the primes, it prints the longest sequences.
  • func main() calls the longestSeq function to find the longest sequences of ascending and descending prime differences.

Source code in the go programming language

package main

import (
    "fmt"
    "rcu"
)

const LIMIT = 999999

var primes = rcu.Primes(LIMIT)

func longestSeq(dir string) {
    pd := 0
    longSeqs := [][]int{{2}}
    currSeq := []int{2}
    for i := 1; i < len(primes); i++ {
        d := primes[i] - primes[i-1]
        if (dir == "ascending" && d <= pd) || (dir == "descending" && d >= pd) {
            if len(currSeq) > len(longSeqs[0]) {
                longSeqs = [][]int{currSeq}
            } else if len(currSeq) == len(longSeqs[0]) {
                longSeqs = append(longSeqs, currSeq)
            }
            currSeq = []int{primes[i-1], primes[i]}
        } else {
            currSeq = append(currSeq, primes[i])
        }
        pd = d
    }
    if len(currSeq) > len(longSeqs[0]) {
        longSeqs = [][]int{currSeq}
    } else if len(currSeq) == len(longSeqs[0]) {
        longSeqs = append(longSeqs, currSeq)
    }
    fmt.Println("Longest run(s) of primes with", dir, "differences is", len(longSeqs[0]), ":")
    for _, ls := range longSeqs {
        var diffs []int
        for i := 1; i < len(ls); i++ {
            diffs = append(diffs, ls[i]-ls[i-1])
        }
        for i := 0; i < len(ls)-1; i++ {
            fmt.Print(ls[i], " (", diffs[i], ") ")
        }
        fmt.Println(ls[len(ls)-1])
    }
    fmt.Println()
}

func main() {
    fmt.Println("For primes < 1 million:\n")
    for _, dir := range []string{"ascending", "descending"} {
        longestSeq(dir)
    }
}


  

You may also check:How to resolve the algorithm Weird numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Oforth programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Salmon programming language
You may also check:How to resolve the algorithm Empty string step by step in the Prolog programming language
You may also check:How to resolve the algorithm Pisano period step by step in the Phix programming language