How to resolve the algorithm Equal prime and composite sums step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Equal prime and composite sums step by step in the Go programming language

Table of Contents

Problem Statement

Suppose we have a sequence of prime sums, where each term Pn is the sum of the first n primes.

Further; suppose we have a sequence of composite sums, where each term Cm is the sum of the first m composites.

Notice that the third term of P; P3 (10) is equal to the second term of C; C2 (10);

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Equal prime and composite sums step by step in the Go programming language

This code is written in Go and finds the first pair of prime sums and composite sums that are equal. First, it creates a prime sieve, which is a data structure used to determine whether or not a number is prime. Next, it initializes two slices, compSums and primeSums, which will store the sums of composite numbers and prime numbers, respectively. It then iterates through the numbers up to the limit, adding each composite number to compSums and each prime number to primeSums.

For each prime sum, it searches for the corresponding composite sum in compSums using the sort.SearchInts function. If the composite sum is found, it prints the prime sum, the order of the prime sum, and order of the composite sum.

The ord function is used to format the order of the numbers. It takes a number as input and returns a string representing the order of the number. For example, it returns "1st" for 1, "2nd" for 2, "3rd" for 3, and so on.

The output of the program is a list of pairs of prime sums and composite sums that are equal.

Source code in the go programming language

package main

import (
    "fmt"
    "log"
    "rcu"
    "sort"
)

func ord(n int) string {
    if n < 0 {
        log.Fatal("Argument must be a non-negative integer.")
    }
    m := n % 100
    if m >= 4 && m <= 20 {
        return fmt.Sprintf("%sth", rcu.Commatize(n))
    }
    m %= 10
    suffix := "th"
    if m == 1 {
        suffix = "st"
    } else if m == 2 {
        suffix = "nd"
    } else if m == 3 {
        suffix = "rd"
    }
    return fmt.Sprintf("%s%s", rcu.Commatize(n), suffix)
}

func main() {
    limit := int(4 * 1e8)
    c := rcu.PrimeSieve(limit-1, true)
    var compSums []int
    var primeSums []int
    csum := 0
    psum := 0
    for i := 2; i < limit; i++ {
        if c[i] {
            csum += i
            compSums = append(compSums, csum)
        } else {
            psum += i
            primeSums = append(primeSums, psum)
        }
    }

    for i := 0; i < len(primeSums); i++ {
        ix := sort.SearchInts(compSums, primeSums[i])
        if ix < len(compSums) && compSums[ix] == primeSums[i] {
            cps := rcu.Commatize(primeSums[i])
            fmt.Printf("%21s - %12s prime sum, %12s composite sum\n", cps, ord(i+1), ord(ix+1))
        }
    }
}


  

You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Panda programming language
You may also check:How to resolve the algorithm Dot product step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Seed7 programming language
You may also check:How to resolve the algorithm System time step by step in the Perl programming language
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the Python programming language