'how to simplimize my go script because always get time out in hackerrank

I have a test interview as a Go Developer and have to do some of the tasks on hackerrank. I've done the task, but when I submit my script it always "times out".. maybe because there are a lot of loops that I use to do this function, and the task is :

enter image description here

So, my solution are :

  1. Loop from a to b with a increment.
  2. Define the digit sum with modulus by 10, sum the result with the leftover.
  3. Define the square sum with converting int(a) to string then use for-range to sum the values.
  4. checking if digit sum and square sum is a prime number, if so then count++

My script is :

func main() {
    fmt.Printf("Jadi ada %d bilangan prima \n", luckyNumbers(1, 20))
}


func luckyNumbers(a int64, b int64) int64 {
    count := 0

    for min, max := a, b; min <= max; min++ {
        squareSum := digitSquare(min)
        digitSum := digitSum(min)

        if isPrime(digitSum) && isPrime(squareSum) {
            count++
        }
    }

    return int64(count)
}

func digitSquare(number int64) int64 {
    numStr := strconv.Itoa(int(number))
    var firstDigit, secondDigit int

    for _, digit := range numStr {
        numInt, _ := strconv.Atoi(string(digit))
        pow := int(math.Pow(float64(numInt), 2))
        if firstDigit == 0 {
            firstDigit += pow
        } else {
            secondDigit += pow
        }
    }

    squareSum := int64(firstDigit + secondDigit)

    return squareSum
}

func digitSum(number int64) int64 {
    var remainder, sumResult int64 = 0, 0

    for number != 0 {
        remainder = number % 10
        sumResult += remainder
        number /= 10
    }

    return sumResult
}

func isPrime(num int64) bool {
    if num < 2 {
        return false
    }

    for i := int64(2); i <= int64(math.Sqrt(float64(num))); i++ {
        if num%i == 0 {
            return false
        }
    }

    return true
}

The script above is the best script that I can make right now, I understand that I do a lot of iterations, so when I try to submit it will always show "time out". So I want to learn from you and want to see if there is a simpler script so that it can be submitted.

Thank you,

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source