Share your repls and programming experiences

← Back to all posts
π in Ruby
h
AmazingMech2418 (894)

I think I'm pretty much doing pi approximations in every language now, so here it is in Ruby!

Here are the other approximations so far:
Java - https://repl.it/talk/share/p-in-Java/34978
QBasic - https://repl.it/talk/share/p-in-QBasic/34973
Fortran - https://repl.it/talk/share/p-in-Fortran/34890
APL - https://repl.it/talk/share/p-in-APL/34888
Forth - https://repl.it/talk/share/p-in-Forth/34652
LOLCODE - https://repl.it/talk/share/p-in-LOLCODE/34421
Python and Node.js (Node is linked in post) - https://repl.it/talk/share/2-Approximations-of-p-In-Only-Four-Lines-of-Code/32773
C - https://repl.it/talk/share/p-Approximations-in-C/33461

Credit to @TheForArkLD , @Warhawk947 , and @LizFoster for inspiring these approximations. Most of these use the Nilakantha Series, although I also use the arctangent method (multiplying the radian arctangent of 1 by 4 to get pi and getting the arctangent with an infinite series) in three of them (Python, Node, and C).

Commentshotnewtop
[deleted]

OMG how many languages you know???
and why you love pi so much???

AmazingMech2418 (894)

@Zuhdi28 I know quite a few languages. I've actually lost count by now... However, I'm not fluent in all the languages I know. Of course, I have some language preferences that cause me to use those languages more, learn more about those languages, and have more practice with those specific languages, where, after enough time, I'm fluent in those languages. Some (not all) of these languages are JavaScript, Python, C/C++ and Arduino (modified C++), Bash, Batch, BASIC, and Java. However, notice that all of these (besides BASIC) are great for certain areas, but not others. For example, JavaScript is great for web development, server development, and command line app development, but is not the fastest language. Python is among the best for cryptography and data science (of course, Nim and Julia, both which I have yet to learn, are currently projected to be more useful in data science in the future...). C and C++ are the fastest object-oriented languages that exist since they are compiled (although they are slower than Fortran and Forth as I recently discovered). Arduino is specialized for microprocessors and can be quickly converted to Energia (just changing library names...) for other boards like the MSP430. Bash and Batch are both great for quick file manipulation scripts in Linux and Windows respectively. BASIC, of course, is mostly outdated, but it is just so basic that I was able to learn it in a day. And finally, Java is great for Android app development (and also is used in an AP class that I will take next year). Really, if you want to learn as many languages as I know, I'd recommend not learning from a tutorial, but from examples combined with documentation. I've found that it is easier for me to learn by reading an example and figuring out what it does than to read a book or an article on a specific programming language. I'm pretty sure it has to do with developing neuroplasticity. Also, of course, you don't read a book to learn a foreign language and programming languages are (or at least should be) no different.

And for pi, it is clearly the best mathematical constant that exists and I really enjoy math, so, my fascination with pi is simply derived from my interest in math.

[deleted]

@AmazingMech2418 and i am crazy to read this...

AmazingMech2418 (894)

@Zuhdi28 I really kind of went off topic in explaining the usefulness of each of the languages I'm fluent in...

LizFoster (606)

@AmazingMech2418 Hahahaha, and he thinks you love π?! Kehehehehehehe...

AmazingMech2418 (894)

@LizFoster LOL! Are you close to finishing your fourth approximation compilation?

AmazingMech2418 (894)

@LizFoster Also, how is your Pokemon game going?

LizFoster (606)

@AmazingMech2418 Both are going great! I'm putting the last one or two into the π approximations compilation 4. I haven't had much time to get onto the Pokemon game though, things have been pretty hectic lately... T~T

[deleted]

@LizFoster lol yes

theangryepicbanana (1629)

Why are you using global variables? Maybe use a more object-oriented approach like this?

# It turns out that Sonic Pi is based on Ruby, so I already know the basic syntax for Ruby...
class ApproxPi
    def initialize()
        @pi = 3
        @a = 2
        @s = 1
    end
    
    def iterate()
        @pi += @s*(4.0/(@a*(@a*(@a+3.0)+2.0)))
        @a += 2
        @s *= -1
    end
end

approx_pi = ApproxPi.new()

150000.times do
    approx_pi.iterate()
end

puts "pi = #{approx_pi.pi}"

As cool as it is to do this in tons of languages (which I fully support btw), maybe try learning a bit more about each language so each approximation is coded in the "style" of that language (if that makes sense).
Also sonic pi is cool :)

AmazingMech2418 (894)

@theangryepicbanana The reason I did global variables is because I did not yet know how to do classes in Ruby (of course, I do now that you have shown me this example). Also, with learning a bit more about each language, I completely understand that. My idea behind this, though, was to learn the basics of as many languages as possible and to learn what the languages are best for so that I can learn more about the languages in the future. For example, while I don't think I will use Ruby as much since it is sort of becoming outdated, it is good to have a general idea of the syntax so that I can program in it if I ever need to. Also, by the style of the languages, I'm guessing you mean like how, in the Java approximation, I used a class to store the functions instead of just using a for loop or while loop. If so, I've been trying to do that as much as possible, but just with Ruby, I did not yet really know what that style was. I thought it was more like what I have done in the past with Sonic Pi where I normally used global variables instead of classes.

AmazingMech2418 (894)

Next, should I do Rust or Go? I'd have to learn both to do them, so I just want to know which to start with.

Jakman (450)

@AmazingMech2418 would be cool if done in F# by .NET Microsoft
would be nice to see other F# projects since i plan on learning all .NET languages.

AmazingMech2418 (894)

@Jakman I'm currently thinking about doing it in as many languages as possible, so maybe F# some time in the future?

Jakman (450)

@AmazingMech2418 cool the syntax is slightly weird. The language is meant to break security after all. But i think one with your expertise would enjoy it.

AmazingMech2418 (894)

@Jakman If you think F# is weird, try APL!

Jakman (450)

@AmazingMech2418 i will do that in the next 10 minutes. Thank you for the suggestion.

AmazingMech2418 (894)

@Jakman Do you know what type of Assembly Repl.it supports? I copied and pasted a "Hello world" program to start learning it and it didn't work.

Jakman (450)

@AmazingMech2418 I dont know. You may want to ask a mod abt that. It might even run on C. idk

AmazingMech2418 (894)

@Jakman I tried x86 and it said there was a format error.

Jakman (450)

@AmazingMech2418 i dont know what to tell you man. Ask for it in the Ask column. Someone on this site will answer.

Jakman (450)

@AmazingMech2418 good this will bring attension to the matter.

AmazingMech2418 (894)

@Jakman Can you comment or upvote to get it on the top of the list? My C pi approximations actually ended up never getting an upvote... I don't want that to be the fate of the ask post since then, people wouldn't answer.

Jakman (450)

@AmazingMech2418 yes. I will also make a bot to upvote it possibly.

theangryepicbanana (1629)

@AmazingMech2418 @Jakman While F# is awesome, upvote botting is not. Please do not discuss anything related to botting or you will both be banned from using Repl Talk. We take this stuff seriously so please take this as a warning and don't go further :)