Skip to content
← Back to Community
π in Swift
Profile icon
AmazingMech2418

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!

Voters
Profile icon
G0RG3
Profile icon
incognitocoder
Profile icon
DigitCommander
Profile icon
BobTheTomatoPie
Profile icon
ANDREWVOSS
Profile icon
Jakman
Profile icon
HahaYes
Profile icon
TheForArkLD
Profile icon
theangryepicbanana
Profile icon
Navinor
Comments
hotnewtop
Profile icon
Codemonkey51

Cool

Profile icon
AmazingMech2418

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

Profile icon
Codemonkey51

Yep I check new :) @AmazingMech2418

Profile icon
AmazingMech2418

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

Profile icon
Codemonkey51
Profile icon
AmazingMech2418

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

Profile icon
Codemonkey51

lol @AmazingMech2418 also congrats on 300 cycles

Profile icon
AmazingMech2418

@Codemonkey51 Thank you!

Profile icon
HahaYes

Ah, stealing my thunder I see

Profile icon
AmazingMech2418

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

Profile icon
HahaYes

@AmazingMech2418 Ah, you chose the easier path.

Profile icon
DigitCommander

Awesome

Profile icon
AmazingMech2418

@DigitCommander Thank you!

Profile icon
TheForArkLD

Cool!I viewed your all pi!

Profile icon
AmazingMech2418

@TheForArkLD Thank you!

Profile icon
TheForArkLD

@AmazingMech2418

** :) **

Profile icon
TheForArkLD

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

Profile icon
AmazingMech2418

@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.

Profile icon
TheForArkLD

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

Profile icon
Navinor

noice

Profile icon
AmazingMech2418

@Warhawk947 Thank you!

Profile icon
NoelB33

This ones pretty fast also

Profile icon
AmazingMech2418

@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.

Profile icon
NoelB33
Profile icon
AmazingMech2418

@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.

Profile icon
NoelB33

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

Profile icon
AmazingMech2418

@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.

Profile icon
NoelB33
Profile icon
Jakman

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

Profile icon
AmazingMech2418

@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.

Profile icon
HahaYes

Binary search algorithm sucks btw