How to find A and B where A and B are prime numbers and A * B = 12349 in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

// Function to check if a number is prime
func isPrime(n int) bool {
    if n <= 1 {
        return false
    }
    if n <= 3 {
        return true
    }
    if n%2 == 0 || n%3 == 0 {
        return false
    }

    limit := int(math.Sqrt(float64(n)))
    for i := 5; i <= limit; i += 6 {
        if n%i == 0 || n%(i+2) == 0 {
            return false
        }
    }

    return true
}

// Helper class to return A and B together
type Pair struct {
    A int
    B int
}

// Function to find the two prime factors A and B
func findAB(N int) Pair {
    limit := int(math.Sqrt(float64(N)))

    for i := 2; i <= limit; i++ {
        if N%i == 0 {
            j := N / i
            if isPrime(i) && isPrime(j) {
                return Pair{A: i, B: j}
            }
        }
    }

    return Pair{A: -1, B: -1} // No prime factors found
}

func main() {
    N := 12349

    result := findAB(N)

    if result.A != -1 {
        fmt.Println("A =", result.A, ", B =", result.B)
    } else {
        fmt.Println("Not found.")
    }
}


/*
run:

A = 53, B = 233

*/

 



answered 11 hours ago by avibootz

Related questions

...