this post was submitted on 29 Jan 2025
34 points (90.5% liked)

Programming

17978 readers
426 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

cross-posted from: https://lemmy.world/post/24857168

I would like to code for a living and to contribute to open source projects and things, but my coding skills are absolute shit after taking online courses and watching video tutorials. How can I learn to code for real?

What I would like to learn is algorithms, web development ("full stack"), how layouts work (both in like kotlin compose and HTML) and how to read other peoples code. Maybe thats more than I can chew, but its probably good for me to try out many things before getting settled on one.

Now I have been coding for a while already (~ 4 years), but I kind of feel like I need more guidance to be able to actually create code that works as intended intentionally, and not through trial and error / stack overflow. As for what level i am at, CS50 is probably my only qualification, I have played around with APIs (I.E. making discord bots), and made some html "apps" (horribly made, but things like the "genius" game and a calculator) and "prototype" react websites (as in, really bare bones, barely working).

I do plan on taking CS or something similar, but i'm not yet in college, and I would like to have a good head start before getting there.

Sorry for my bad English, and any help is appreciated.

you are viewing a single comment's thread
view the rest of the comments
[–] over_clox 2 points 1 day ago* (last edited 1 day ago) (2 children)

I'll be random and toss you a BASIC bone..


FUNCTION CalcPI#
    X# = 0: Y# = 1
    FOR N% = 1 TO 26
        R# = SQR(X# * X# + Y# * Y#): X# = X# / R#: Y# = Y# / R#
        P# = (2 ^ N%) * SQR((X# - 1) ^ 2 + Y# * Y#): X# = X# + 1
    NEXT
    CalcPI# = P#
END FUNCTION

Figure it out, or not. 26 is not exactly fixed, the higher that number, the more accurate π is...

[–] ChilledPeppers 2 points 1 day ago (3 children)

I will try to guess how that works!

keep in mind that I have no clue on how BASIC sintax works lol

1 -> function declared
2 -> variable x and y initiated to values 0 and 1.
3 -> for loop created that goes from 1 to 26 (updating variable N)
4 -> Variable R is created, its value is the square root of (x^2 + y^2). Xs and Ys values are set to be themselves divided by R.
5 -> P variable created (it stores the value of PI), and set to (2 ^ N) * square root of (x - 1) ^ 2 + y ^ 2. Lastly X is increased by one
6 -> Proceed only after for loop is done
7 -> return the variable P

(I do recognize I just transcribed the code, and I also made an implementation in python of it, lol)

now for why that works ill figure another time, its 12:30 pm here and I am eepy :). But I have recognized that X tends to 2 and Y to 0.

and I am pretty sure this implements the algorithm of creating higher and higher "resolution" circles so starting with a triangle and adding more and more vertices (vertex?). Ill edit in the full explanation late :)

here is my implementation:


def CalcPi():
    X = 0
    Y = 1
    P = 0

    for i in range(1,26):
        R = sqrt(X**2 + Y**2)
        
        X = X/R
        Y = Y/R

        P = (2**i) * sqrt((X-1)**2 + Y**2)
        X += 1
    return P
[–] over_clox 1 points 1 day ago

Proceed way past 26 if you want more accuracy, assuming the CPU can handle more accuracy (most likely these days)..

[–] over_clox 1 points 1 day ago

I'm impressed!

Too lazy to test your version of the code, cuz I don't think I even need to. You understand the code from the start!

Wonderful! I love my π circles round...

[–] over_clox 1 points 1 day ago

Looks perfect to me!

Hell, resembles my first variation of the code back around the year 1999

[–] [email protected] 2 points 1 day ago (1 children)

I believe the formatting issue is because the triple back tick is on a line with other text. In most markdown flavors triple backticks should be on blank line (with some exceptions for hinting at syntax highlighting). Slack's is the only dialect I've seen that specifically strips newlines from triple backticks and I hate it.

[–] over_clox 2 points 1 day ago

I just tried an edit to add blank lines before and after the backticks, now happy formatting!