From c338f0a92c56d33d76c55aa522428b5403543368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Tue, 2 Apr 2019 22:36:08 +0200 Subject: [PATCH] Create isprime.go --- functions/isprime.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 functions/isprime.go diff --git a/functions/isprime.go b/functions/isprime.go new file mode 100644 index 0000000..9660cf9 --- /dev/null +++ b/functions/isprime.go @@ -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 +}