Skip to content

Instantly share code, notes, and snippets.

@tscholl2
Created August 17, 2020 16:05
Show Gist options
  • Save tscholl2/bf83c20c7b614eb27151230c7bb71061 to your computer and use it in GitHub Desktop.
Save tscholl2/bf83c20c7b614eb27151230c7bb71061 to your computer and use it in GitHub Desktop.
AWS lambda example in Go
// Testrun with:
// go build *.go && docker run --rm -v "$PWD":/var/task lambci/lambda:go1.x main '{"prefix":"23727483927892"}'
// Deploy by:
// zip main.zip main
// upload main.zip to https://console.aws.amazon.com/lambda/home
package main
import (
"context"
"github.com/aws/aws-lambda-go/lambda"
)
func handleRequest(ctx context.Context, event map[string]string) (string, error) {
return Prefix(event["prefix"])
}
func main() {
lambda.Start(handleRequest)
}
package main
import (
"errors"
"math/big"
"strings"
"github.com/tscholl2/prime/prime"
)
var (
base = 10
)
// Prefix takes a integer N in base 10 as a string,
// and returns a prime number whose digits start with N.
func Prefix(N string) (string, error) {
a, ok := new(big.Int).SetString(N, base)
if !ok {
return "", errors.New("unable to read prefix integer")
}
if a.Sign() <= 0 {
return "", errors.New("prefix number must be positive")
}
return nextPrimeWithPrefix(a).Text(base), nil
}
func nextPrimeWithPrefix(N *big.Int) *big.Int {
prefix := N.Text(base)
a := new(big.Int).Set(N)
b := big.NewInt(int64(base))
for {
p := prime.NextPrime(a)
if strings.HasPrefix(p.Text(base), prefix) {
return p
}
a.Mul(a, b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment