Anyone ever take a long time to get down how your code works when learning it...

Anyone ever take a long time to get down how your code works when learning it? Took me almost 10 hours and a lot of frustration to figure this one out for some reason.

Attached: A99B12E9-ED64-4362-8569-AE7ACC05F156.jpg (4032x3024, 3.3M)

Other urls found in this thread:

en.m.wikipedia.org/wiki/Superior_highly_composite_number
book.pythontips.com/en/latest/for_-_else.html
twitter.com/AnonBabble

It takes time to develop mental heuristics for computational operations. Just stick to it.

U hope you're new if it took you 10 hours to grok that

This one pretty simple boss. Don't forget don't shit in street or can lose visa and no bobs

Your next task:
Improve your code so it won't iterate less than range(2, n) to determine if a number is a prime.

>it won't iterate
it iterates

This is probably bait, but if you’re serious: the % is a modulo operator, which gets the remainder of a (floored) division.
Eg: 10 / 3 = 3, 10 % 3 = 1
If you can perfectly divide two numbers, there is no remainder, so modulo returns 0. Prime numbers are only divisible by 1 and themselves.
The code checks if that number % range(1, num) = 0, and if so, concludes that it is not a prime number.

Since you can't even figure out how to take a screenshot, it's totally unsurprising that this is too much for you.

Every time I do an exercise like that (I'm also learning) I try to do a version that accepts user input. 2-10 is easy.

It's not your code if you don't know how it works.

This

Theese

inner loop of (2, n) only needs to go to the square root of n.

Even better is the sieve of Eratosthenes

took me 5 seconds lol

whats the oposite to prime numbers if there is any

You only need to go to the sqrt(n)

Composite numbers

composite numbers

Disagree; "not prime" isn't the same thing as "opposite of prime".

I suppose that would be highly composite numbers or superior highly composite numbers. en.m.wikipedia.org/wiki/Superior_highly_composite_number

Composite number mind
Whatever, nerd

your indents are confusing but it werks

YES BUT THAT WAS POINTERS AND STRUCT

Don't you only need to loop from 2-(n/2) in that second for loop?

Am I too stupid or is the output seems bogus?

Attached: code.png (578x585, 19K)

>stupid
you're not mimicking python's 'range' behaviour at all

Better than not knowing how your code works, which is how most AI is done

I have barely any idea how to code and only did some VERY light visual basic like 5 years ago okay and I think I could figure this out in under an hour. I just have to look up what some of the expressions like the % sign and double equals then maybe the arguments for some of the commands. In reality I'd probably have its function figured out in under 20 minutes. How much of brainlet do you have to be for this to take 10 hours?

also the else belongs to the for loop, not the if

book.pythontips.com/en/latest/for_-_else.html

So, python's for loop can include an else statement which runs if it successfully iterated the whole list without hitting break?
Neat but seems impractical. This just blows away what I expected when reading the code at first try.

yeah python is unconventional in so many ways

You won't know if you just copy/paste from some stackoverflow thread.

for (var n = 2; n

>10 hours
this is why cs is considered a joke

Im new to python can anyone explain me how the for loop in a for loop works?
for n in range(2, 10):
for x in range(2, n):
print('1')
....

does the first for loop, loop itself 8 times and how the second loop works?

To be honest I'm a compsci grad who writes .NET for a living and that shit looks like gibberish lol. If I ever need to do stuff like this I just copy code from Stackoverflow

Same as it works in all other languages

A living cs grad meme

I code shit all the time that I don't understand. If it works, who cares?

the second argument in non inclusive

Remember op - most of your time as a new programmer should be spent optimizing your code as much as possible. Your cpu only has a limited number of cycles (similar to the human heart) so be very careful not to waste any.

do none of you idiots realize you only need to check up to sqrt(n)?

bruh this took me 10 seconds to understand nothing is hard about it

thanks user i didn't know about the for else construct
these millenials' languages are pretty interesting

do you not know how modulus works or was it the loops confusing you

>check out my basic programming skills
you also don't need to check even numbers
in fact the numbers you need to check will be below sqrt(n) and of the form 6k +/- 1, for k = 1, 2, ...
imagine how much more we can optimize the example meant to display the for-else construct.

It literally isn't his code, it is from book.pythontips.com/en/latest/for_-_else.html

What language is this? Been programming for about a month only competent in C.
Also OP I think you have dumb, that program is pretty obvious.

See post above yours

Did you see the link I posted?
Here's the code in that link, if you're afraid to click links.
using System;

public static class PrimeTool
{
public static bool IsPrime(int candidate)
{
// Test whether the parameter is a prime number.
if ((candidate & 1) == 0)
{
if (candidate == 2)
{
return true;
}
else
{
return false;
}
}
// Note:
// ... This version was changed to test the square.
// ... Original version tested against the square root.
// ... Also we exclude 1 at the end.
for (int i = 3; (i * i)

>checking even numbers after checking 2
wew, nice O(n^2) algo you got there, just look up Sieve of Eratosthenes

why does every thread in Jow Forums devolve into newfags missing the point of the OP.is it because grade school is out?

>someone posts code
>everyone in thread tries to post their version
welcome to Jow Forums

Stop using toy languages.

it's like hey just passing through. it's not like i'm gonna read the whole thread then add something of value lol

>Took me almost 10 hours and a lot of frustration to figure this one out for some reason
how?

we were all 12 once user

its 7 lines of code

the "finished" product was yes, but think of how many logic and syntax errors OP had until he googled an answer on stack-overflow to post on Jow Forums