Create isprime.go

This commit is contained in:
Torma Kristóf 2019-04-02 22:36:08 +02:00 committed by GitHub
parent 2f1585a427
commit c338f0a92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

27
functions/isprime.go Normal file
View File

@ -0,0 +1,27 @@
package kubeless
import (
"fmt"
"math"
"strconv"
"github.com/kubeless/kubeless/pkg/functions"
"github.com/sirupsen/logrus"
)
func IsPrime(event functions.Event, context functions.Context) (string, error) {
num, err := strconv.Atoi(event.Data)
if err != nil {
return "", fmt.Errorf("Failed to parse %s as int! %v", event.Data, err)
}
logrus.Infof("Checking if %s is prime", event.Data)
if num <= 1 {
return fmt.Sprintf("%d is not prime", num), nil
}
for i := 2; i <= int(math.Floor(float64(num)/2)); i++ {
if num%i == 0 {
return fmt.Sprintf("%d is not prime", num), nil
}
}
return fmt.Sprintf("%d is prime", num), nil
}