Skip to content
← Back to Community
π in C#
Profile icon
AmazingMech2418

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

Here are the other approximations so far:
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).

Voters
Profile icon
TrentPacker1
Profile icon
LaizaNeves
Profile icon
BobTheTomatoPie
Profile icon
HahaYes
Profile icon
ANDREWVOSS
Profile icon
Jakman
Profile icon
TheForArkLD
Profile icon
Navinor
Profile icon
CodingCactus
Profile icon
ChezCoder
Comments
hotnewtop
Profile icon
ChezCoder

Wow, never knew there was so much to pi!

Profile icon
AmazingMech2418

@ChezCoder Well, regardless of what @StudentFires may say, pi is the best mathematical constant!

Profile icon
ChezCoder

@AmazingMech2418 definitely!

Profile icon
xxpertHacker

@AmazingMech2418 Well, regardless of what AmazingMech2418 may say, neither Pi, nor Tau, are the best mathematical constants!

Profile icon
ChezCoder

@StudentFires :O! HOW COULD YOU!!!!????????

Profile icon
xxpertHacker

@ChezCoder Easily? It was relatively easy.

Profile icon
ChezCoder

@StudentFires :O!!!!!!!

Profile icon
HahaYes

@AmazingMech2418 No e is the best mathematical constant

Profile icon
AmazingMech2418

@HahaYes Euler's identity requires pi though...

Profile icon
ChezCoder
Profile icon
HahaYes

@AmazingMech2418 No Euler's number

Profile icon
AmazingMech2418

@HahaYes Yes, but all of its usefulness lies in the identity.

Profile icon
AmazingMech2418

@HahaYes Without Euler's identity, you wouldn't have imaginary exponents or natural logarithms or anything like that.

Profile icon
HahaYes
Profile icon
AmazingMech2418

@HahaYes And the identity is based on radian trigonometry, so it requires pi.

Profile icon
HahaYes

@AmazingMech2418 but Tau is better than pi

Profile icon
AmazingMech2418

@HahaYes Is it e^iτ or e^iπ?

Profile icon
HahaYes

@AmazingMech2418 bruh just because pi is used more doesn't mean it is better

Profile icon
HahaYes

@AmazingMech2418u is so much better for arcs, radians, and anything related to a circle.

Profile icon
AmazingMech2418

@HahaYes Just because tau saves one simple step of multiplication in one formula doesn't make it better.

Profile icon
HahaYes

@AmazingMech2418 Omg is this what nerds do? Have flame wars on math?

Profile icon
AmazingMech2418

@HahaYes That's pretty much what we do...

Profile icon
HahaYes
Profile icon
AmazingMech2418

@HahaYes Pi vs tau is a large debate... Although it shouldn't be one at all because pi is obviously better.

Profile icon
AmazingMech2418

@HahaYes By the way, JavaScript > Python. Another huge debate...

Profile icon
LizFoster

@AmazingMech2418 That's just an incorrect debate :)

Profile icon
AmazingMech2418

@LizFoster LOL! Python is still good, just JS is better... Can you use Python natively in the browser? Also, while Python can do it too, JS is also used for desktop applications (Electron) and command line applications (Node) while React Native can also be used on mobile with JS (I don't think Python can match that).

Profile icon
AmazingMech2418

@LizFoster And, please, don't use the TIOBE index as your reason why Python is better. JS is just underrated (should be 1st, not 7th).

Profile icon
LizFoster
Profile icon
HahaYes

@AmazingMech2418 But Python has more users

Profile icon
AmazingMech2418

@HahaYes Well, let's just leave it at Python is best for machine learning and data science and JS for pretty much everything else if you don't care about speed (C is best for speed).

Profile icon
HahaYes

@AmazingMech2418 That's why I use C++

Profile icon
HahaYes

@AmazingMech2418 I've never like Javascript. It was hyped wayyy to much.

Profile icon
AmazingMech2418

@HahaYes How could you?

Profile icon
AmazingMech2418

@HahaYes How dare you insult the greatest programming language of all time?

Profile icon
HahaYes

@AmazingMech2418 Name one person who uses Javascript in a coding competition.

Profile icon
AmazingMech2418

@HahaYes Me...

Profile icon
HahaYes

@AmazingMech2418 The greats such as Gennady Korotkevich all use C++

Profile icon
HahaYes

@AmazingMech2418 Javscript is kind of a ewie langauge. Go learn Java

Profile icon
AmazingMech2418

@HahaYes C is better than C++ anyways...

Profile icon
HahaYes

@AmazingMech2418 C++ is better than C

Profile icon
HahaYes

@AmazingMech2418 C++ is just harder. Also I use VIM

Profile icon
AmazingMech2418

@HahaYes LOL! You're funny! You want me to learn a language I already know that I also can barely stand because it is a worse programming language than JS!

Profile icon
HahaYes

@AmazingMech2418 My man, the older the language, the better it is. It is like wine

Profile icon
AmazingMech2418

@HahaYes
You: C++ is just harder.
Me: C - char arrays, C++ - actual STRINGS. Which is harder again?

Profile icon
HahaYes

@AmazingMech2418 C is a general purpose language. C++ is a pure general purpose language.

Profile icon
AmazingMech2418

@HahaYes Nano > Vim, C > C++, JS > Python, Pi > Tau (not mathematically, but in that pi is better), Java < literally everything else.

Profile icon
HahaYes

@AmazingMech2418 Vim is an IDE bro

Profile icon
Jakman

@HahaYes that is right. I started with python and moved on to C#. It is a great transition to harder langs. I understand C to an extent and it looks like art to me.

Profile icon
AmazingMech2418

@HahaYes I like older languages too (C, Forth, Fortran, BASIC, etc.) but Java stinks. Security issues (remember when people got hacked in Minecraft) and already about to lose to Kotlin in Android development.

Profile icon
AmazingMech2418

@HahaYes So is Nano... Though real coders use Notepad...

Profile icon
HahaYes

@AmazingMech2418 NOTEPAD IS FOR WINDOWS USERS

Profile icon
AmazingMech2418

@ChezCoder I already saw it... You don't need to literally advertise everywhere.

Profile icon
ChezCoder

@AmazingMech2418 im getting like 50 pings from this :(

Profile icon
HahaYes

@AmazingMech2418 I've never liked Java, I was making a joke

Profile icon
AmazingMech2418

@HahaYes LOL! And, I'm also a Windows user and a Linux user...

Profile icon
AmazingMech2418

@ChezCoder
image

Profile icon
HahaYes

@ChezCoder As you can see Chez, we have a flame war about coding.

Profile icon
ChezCoder

@HahaYes lol yes i can see xD

Profile icon
HahaYes

@AmazingMech2418 I'm a ubuntu boi

Profile icon
AmazingMech2418

@HahaYes I have Ubuntu MATE on my Raspberry Pi 3B+. I'm going to download Kali on my Pi 4 and still need to figure out for my other Pi 3B+.

Profile icon
HahaYes

@AmazingMech2418 Windows doesn't care about developers.

Profile icon
HahaYes

@AmazingMech2418 Ah, you have a raspberry pi! Cool!

Profile icon
AmazingMech2418

@HahaYes VS Code...

Profile icon
AmazingMech2418

@HahaYes 3, not 1.

Profile icon
HahaYes

@AmazingMech2418 VS Code is horrible. It used to be great, but now it is so bloated

Profile icon
HahaYes

@AmazingMech2418 That was a exclamation mark

Profile icon
AmazingMech2418

@HahaYes Honestly, I just use Repl.it, GitHub, Notepad, and of course Nano on my Raspberry Pi 3B+ and my Android phone running Termux.

Profile icon
HahaYes

@AmazingMech2418 cool, get a raspberry pi 4b?

Profile icon
HahaYes

@AmazingMech2418 Finally. The war is over.

Profile icon
AmazingMech2418

@HahaYes I have a Pi 4 (I think maybe B, I forgot) with 4GB of RAM, but I still have to put an OS on the SD (currently using 32GB, but probably going to upgrade to 128) Unfortunately, Ubuntu MATE doesn't work on my Pi 4 though. I already tried and it didn't work... I even just booted Ubuntu Server, connected to Wifi using commands and USB-added packages to allow those commands to work (don't have Ethernet), and added MATE and it wouldn't work for my monitor. I had to use hdmi_safe as well, so I think it just didn't see the monitor.

Profile icon
HahaYes

@AmazingMech2418 Are you in college or something?

Profile icon
AmazingMech2418

@HahaYes High school...

Profile icon
HahaYes
Profile icon
AmazingMech2418

@HahaYes Thank you!

Profile icon
LizFoster

@AmazingMech2418 WWWWWWWWWWW

So, I have made several optimizations overall, the biggest one so far being that I removed the show() function from a class, which should save some space and memory (I will have to write fewer lines overall). I have also removed the SPRITE class, since it is not in use at the moment, and I would rather worry about it when it is absolutely necessary.

I removed the black border and extra screen size, since it was a pointless addition. Finally, I have removed the MOVEPLAYER class, and the win.onkey commands related to this. It was just taking up space when it was not needed..

Do you have any more suggestions? If need be, I can invite you to the repl.

Profile icon
AmazingMech2418

@LizFoster Well, I don't really have any suggestions as of right now, but it seems that there is a bug in your code. It says there is bad input on line 91. Do you have any idea what caused this?

Profile icon
LizFoster

@AmazingMech2418 Aaah, yes, I do know what happened there.I have started re-implementing the sprites, using your sprite tool, and the line where the sprite should be (line 91) is technically incomplete.

Profile icon
LizFoster

@AmazingMech2418 Oh yes, also, do I need to keep the compileLine() and compileSprite() functions in the game's code? They appear to already be in the sprite tool, so I am unsure..

Profile icon
AmazingMech2418

@LizFoster compileLine() and compileSprite() still convert to the hexadecimal form, so they still are needed, but I guess you could probably just move it into the rendering function.

Profile icon
LizFoster

@AmazingMech2418 Oh, yeah, probably. I'll see what I can do. Thank you!

Profile icon
AmazingMech2418

@LizFoster You're welcome!

Profile icon
TheForArkLD

now i can use C#!

using System; class MainClass { public static void Main(string[] args){ decimal pi = 4; for(decimal i = 1; i < 100000; i++){ pi = pi + (((-1) ** i) * (2*i+1)); } Console.WriteLine(pi); } }
Profile icon
AmazingMech2418
Profile icon
TheForArkLD
Profile icon
staticvoidliam7

C#GANG

Profile icon
Jakman

@LiamDonohue thats right.

Profile icon
LizFoster

@LiamDonohue Hey Liam wwwwwwwww

Profile icon
HahaYes

@LiamDonohue I believe you don't need glasses anymore

Profile icon
HahaYes

I can see sharp now

Profile icon
AmazingMech2418

@HahaYes LOL! Nice joke! By the way, have you ever heard of D flat?

Profile icon
HahaYes
Profile icon
AmazingMech2418

@HahaYes It is the same thing as C#!

Profile icon
HahaYes

@AmazingMech2418 It is like an improvement of C#

Profile icon
HahaYes

@AmazingMech2418 I know but that is a coding language

Profile icon
AmazingMech2418

@HahaYes Wait, that is actually a different language? I was just making a joke...

Profile icon
HahaYes

@AmazingMech2418 Yep type in d flat programming language

Profile icon
AmazingMech2418

@HahaYes I see it now... However, D double-flat is more well-known...

Profile icon
HahaYes

@AmazingMech2418 btw can you check out my very inconsistent pi in Swift project?

Profile icon
AmazingMech2418

@HahaYes Try removing the Very keyword.

Profile icon
HahaYes
Profile icon
Jakman
using System; public class mathpi{ static void Main(string[] args){ int pi; pi = 22/7; Console.Write(pi); } }
Profile icon
AmazingMech2418

@Jakman Well, that's the easy way... Now, try using an algorithm!

Profile icon
ChezCoder
Profile icon
Jakman
Profile icon
[deleted]

This is the best one in my opinion

Profile icon
AmazingMech2418

@FunnyLamma Thank you!

Profile icon
NoelB33

This may be the fastest one yet

Profile icon
AmazingMech2418

@NoelBryan I still think Forth is, but this one is close!

Profile icon
NoelB33

Oh, yeah your right forth is almost instant @AmazingMech2418

Profile icon
AmazingMech2418

@NoelBryan Though, I'm kind of surprised that C# is faster than most others since most other compiler languages compile into ELF executables rather than EXEs which are Windows-only.

Profile icon
AmazingMech2418

@NoelBryan We can also say this is my 314 cycle celebration.
image