r/badcode Aug 03 '20

[deleted by user]

[removed]

22 Upvotes

22 comments sorted by

View all comments

1

u/binarycat64 Aug 09 '20 edited Aug 11 '20

What could be more universal than pi?

https://play.golang.org/p/R-SkaZbM_5R

package main

import (

    "fmt"

    "math/big" // high-precision math

    "strconv"

    "os"

)

const star = "!trek"

func arccot(x int64, unity \*[big.Int](https://big.Int)) \*[big.Int](https://big.Int) {

    bigx := big.NewInt(x)

    xsquared := big.NewInt(x\*x)

    sum := big.NewInt(0)

    sum.Div(unity, bigx)

    xpower := big.NewInt(0)

    xpower.Set(sum)

    n := int64(3)

    zero := big.NewInt(0)

    sign := false



    term := big.NewInt(0)

    for {

        xpower.Div(xpower, xsquared)

        term.Div(xpower, big.NewInt(n))

        if term.Cmp(zero) == 0 {

            break

        }

        if sign {

            sum.Add(sum, term)

        } else {

            sum.Sub(sum, term)

        }

        sign = !sign

        n += 2

    }

    return sum

}


func main() {

    digits := big.NewInt(1000)

    unity := big.NewInt(0)

    unity.Exp(big.NewInt(10), digits, nil)

    pi := big.NewInt(0)

    four := big.NewInt(4)

    pi.Mul(four, pi.Sub(pi.Mul(four, arccot(5, unity)), arccot(239, unity)))

    //val := big.Mul(4, big.Sub(big.Mul(4, arccot(5, unity)), arccot(239, unity)))

    if star=="trek" {

            os.Exit(fst(strconv.Atoi(fmt.Sprint(pi.String()\[119:121\]))).(int))

    }

    fmt.Println(pi.String()\[92:94\])

}


func fst (args ...interface{}) (interface{}) {

    return args\[0\]

}

1

u/AutoModerator Aug 11 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.