Untitled

mail@pastecode.io avatar
unknown
golang
2 years ago
785 B
6
Indexable
Never
package main

import (
	"fmt"
	"math"
)

func checkPrimeNumber(num uint) bool {
	if num < 2 {
		return false
	}
	sq_root := uint(math.Sqrt(float64(num)))
	var i uint = 2
	for ; i <= sq_root; i++ {
		if num%i == 0 {
			return false
		}
	}

	return true
}

func main() {
	var deret uint
	var sumPrime uint
	var foundPrime uint

	fmt.Print("Masukan total deret : ")
	fmt.Scan(&deret)

	if deret <= 1 {
		fmt.Println("Maaf total deret harus lebih dari 1")
	} else {
		var currentNumber uint = 2
		for foundPrime < deret {
			if checkPrimeNumber(currentNumber) {
				sumPrime += currentNumber

				fmt.Printf("%d", currentNumber)
				if foundPrime < deret-1 {
					fmt.Print(" + ")
				} else {
					fmt.Printf(" = %d\n", sumPrime)
				}

				foundPrime++
			}

			currentNumber++
		}
	}
}