Share your repls and programming experiences

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

I think I'm pretty much doing pi approximations in every language now, so, here it is in Swift! By the way, compare this to Kotlin and see why Kotlin is starting to take over Java in Android development (Swift is for Mac and iOS development).

Here are the other approximations so far:
C# - https://repl.it/talk/share/p-in-C/36141
Bash - https://repl.it/talk/share/p-in-Bash/36133
Kotlin - https://repl.it/talk/share/p-in-Kotlin/36125
Ruby - https://repl.it/talk/share/p-in-Ruby/34982
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 @NoelBryan, @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).

Also, thank you to @HahaYes for inspiring this approximation to be done in Swift after attempting your own version with a binary search algorithm!

Commentshotnewtop
AmazingMech2418 (894)

@Codemonkey51 Thank you! By the way, that was fast!

AmazingMech2418 (894)

@Codemonkey51 I do too. Actually, I said that was fast. I should use a better word instead. Maybe "swift"? LOL!

AmazingMech2418 (894)

@Codemonkey51 LOL! I just noticed that I'm on the "hot" page of Repl.it Talk 4 times...

Codemonkey51 (851)

lol @AmazingMech2418 also congrats on 300 cycles

HahaYes (1211)

Ah, stealing my thunder I see

AmazingMech2418 (894)

@HahaYes LOL! Yeah, I guess. However, I did the Nilakantha Series.

HahaYes (1211)

@AmazingMech2418 Ah, you chose the easier path.

TheForArkLD (710)

Cool!I viewed your all pi!

TheForArkLD (710)

@AmazingMech2418 And if you can calculate pi in my own language then i am happy :)

AmazingMech2418 (894)

@TheForArkLD Currently, I'm not even planning on doing it in any of my own languages (XPL, EAGL, Inventor, Adapt, etc.), so, I'm sorry, but I likely won't do it in your language.

TheForArkLD (710)

@AmazingMech2418 hmmmm ok
(but Someday do it please :))

NoelB33 (314)

This ones pretty fast also

AmazingMech2418 (894)

@NoelBryan This one is also a lot like Kotlin. Honestly, after seeing the similarities, I just copied and pasted and changed the for loop and changed println to print.

AmazingMech2418 (894)

@NoelBryan Thank you! The two languages are nearly identical, just that they have different functions, a one-letter difference in function declarations (fun in Kotlin, func in Swift), and different loop and conditional syntaxes.

NoelB33 (314)

Oh, maybe I can learn a bit of Kotlin then, I’m learning a bit of swift also but I’m no good at it @AmazingMech2418

AmazingMech2418 (894)

@NoelBryan If you are better at Python, do Swift. If you are better at JavaScript, C, C++, or any other C-family language with the same syntax, learn Kotlin first.

Jakman (450)

Cool. I dont think you needed a class tho. Is swift a language where everything is done in a class in that case?

AmazingMech2418 (894)

@Jakman I know I don't need a class (could have just done a function), but I plan on eventually creating mobile apps with Kotlin and Swift, so I should know how to do classes in them.

HahaYes (1211)

Binary search algorithm sucks btw