Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to find two prime numbers that, when concatenated, form another prime number in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strconv"
)

// Function to check primality
func isPrime(n int64) bool {
    if n < 2 {
        return false
    }
    if n%2 == 0 && n != 2 {
        return false
    }
    for i := int64(3); i*i <= n; i += 2 {
        if n%i == 0 {
            return false
        }
    }

    return true
}

// Concatenate two integers
func concat(a, b int) int64 {
    s := strconv.Itoa(a) + strconv.Itoa(b)
    num, _ := strconv.ParseInt(s, 10, 64)

    return num
}

func main() {
    limit := 12 // you can increase this
    var primes []int

    // Generate primes up to limit
    for i := 2; i <= limit; i++ {
        if isPrime(int64(i)) {
            primes = append(primes, i)
        }
    }

    // Check pairs
    for i := 0; i < len(primes); i++ {
        for j := 0; j < len(primes); j++ {
            if i == j {
                continue // skip same prime if you want
            }
            num := concat(primes[i], primes[j])
            if isPrime(num) {
                fmt.Printf("%d + %d -> %d is prime\n", primes[i], primes[j], num)
            }
        }
    }
}



/*
run:

2 + 3 -> 23 is prime
2 + 11 -> 211 is prime
3 + 7 -> 37 is prime
3 + 11 -> 311 is prime
5 + 3 -> 53 is prime
7 + 3 -> 73 is prime
11 + 3 -> 113 is prime

*/

 



answered Nov 28, 2025 by avibootz
edited Nov 28, 2025 by avibootz
...