Share your repls and programming experiences

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

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

Commentshotnewtop
ChezCoder (1498)

Wow, never knew there was so much to pi!

AmazingMech2418 (894)

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

xxpertHacker (387)

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

ChezCoder (1498)

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

xxpertHacker (387)

@ChezCoder Easily? It was relatively easy.

HahaYes (1211)

@AmazingMech2418 No e is the best mathematical constant

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

LizFoster (606)

@AmazingMech2418 That's just an incorrect debate :)

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

HahaYes (1211)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

Jakman (450)

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

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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

HahaYes (1211)

@AmazingMech2418 NOTEPAD IS FOR WINDOWS USERS

AmazingMech2418 (894)

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

ChezCoder (1498)

@AmazingMech2418 im getting like 50 pings from this :(

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

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

HahaYes (1211)

@AmazingMech2418 Windows doesn't care about developers.

HahaYes (1211)

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

HahaYes (1211)

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

HahaYes (1211)

@AmazingMech2418 That was a exclamation mark

AmazingMech2418 (894)

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

HahaYes (1211)

@AmazingMech2418 cool, get a raspberry pi 4b?

AmazingMech2418 (894)

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

HahaYes (1211)

@AmazingMech2418 Are you in college or something?

LizFoster (606)

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

AmazingMech2418 (894)

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

LizFoster (606)

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

LizFoster (606)

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

AmazingMech2418 (894)

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

LizFoster (606)

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

TheForArkLD (710)

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);
}
}
HahaYes (1211)

@LiamDonohue I believe you don't need glasses anymore

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

I can see sharp now

AmazingMech2418 (894)

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

AmazingMech2418 (894)

@HahaYes It is the same thing as C#!

HahaYes (1211)

@AmazingMech2418 It is like an improvement of C#

HahaYes (1211)

@AmazingMech2418 I know but that is a coding language

AmazingMech2418 (894)

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

HahaYes (1211)

@AmazingMech2418 Yep type in d flat programming language

AmazingMech2418 (894)

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

HahaYes (1211)

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

AmazingMech2418 (894)

@HahaYes Try removing the Very keyword.

[deleted]

This is the best one in my opinion

NoelB33 (314)

This may be the fastest one yet

AmazingMech2418 (894)

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

NoelB33 (314)

Oh, yeah your right forth is almost instant @AmazingMech2418

AmazingMech2418 (894)

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

AmazingMech2418 (894)

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