Range in is_prime


#1

Perhaps:

range(2, x-1)  # We can assume that x is already divisible by itself .

Why does is_prime fail for some numbers?
#2
range(2, x)  # x never comes into the picture

x is excluded from the range by definition.

>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
>>> 

#3

Ah for sure, I just assumed because I was having trouble with that same lesson myself last night and the solution provided that clarification as part of the result. Makes since how it really wouldn’t be necessary tho I guess to exactly add that portion.


#4

We (the community) have discussed this at length in many threads. A consolation to the oversight of the author is that either still work in this brute force approach.

A brute force approach means try every value, without first considering whether it should be a candidate for the test.

Ask yourself, “What is the largest factor we need to suss out?”

Do we need even numbers (besides 2) as test candidates?

There is a proof (way over my head, don’t know about you) that says that every non-prime is a product of one or more primes.

Can you reduce the test candidate list to 23 numbers for 9199?


#5

Makes sense. That one gave me a hang up for awhile, I felt a little ridiculous after seeing how simple the code was after my massively failed attempt at creating so many if and elif statements to try to solve that algorithm >.<.


#6

The goal of programming is to achieve an objective. That could be along the lines of any discipline, information, science, math, graphics, &c. The more we understand our subject, the easier it is to convey this in a programming language. That’s what this exercise is all about. Learn the subject, and master it, then write the code. Not the other way around.

Eg.

What is the probability that I will pick 6 numbers (all unique) from 1 to 49 that are all drawn in the next 6/49 draw?

Hint: It’s a rather large number (when based on one chance in n; otherwise, it is a very small number), but how do we find it? Math. Probability. Newton. Binomial Theorem. Combinations and permutations. It comes down to,

49 Choose 6

If you do not understand this, then there is no point trying to write code to solve it. That is my point. Not to deride you, but to stress the importance of knowing our subject in the general sense. Sorry you get pulled into it.


#7

Oh I know, I never thought for one second you were deriding me, and I do full-heartily agree that and it’s pretty much one of the main reasons I love learning programming. I think of it like an art of literature, there are numerous grammars, linguistics, and languages for ethnicity’s and cultural communications to express an idea or thought to another. Be it as it may, being able to understand the rudiments of a type of language doesn’t mean you could create poetry.

and if I think i have the right idea guessing out of 50, I would say approx 18,000,000? >.< if thats close? idk
or maybe 12,000,000 :no_mouth: mrah, now I’m obliged to do some math, this will bug me if i don’t figure it out


#8

https://www.wolframalpha.com/input/?i=49C6


#9

Did you do the exercise for factorial, yet? It will come in handy for solving this problem.

nCr = n! / (r! * (n - r)!)
def factorial(n):
    f = 1
    while n > 0:
        f *= n
        n -= 1
    return f
n = 49
r = 6

nCr = factorial(n) / (factorial(r) * factorial(n - r))

print (nCr)    # 13983816.0

#10

Yeah, one of the few I got. I had a couple unnecessary variables tho and was so close to that solution.


#11

I thought it was nCk.:upside_down_face:
iono why I’m thinking k :confused: :weary: :gun:
but yeah think your right

nPr = n! / (n-r)!
nCr = n! / r!(n-r)!

#12

The variables are arbitrary. I learned this decades ago as n Choose r but any variables would be appropriate so long as the math follows through.

Have you ever studied Newton’s Binomial Theorem? In the Wikipedia article it uses n and k in the binomial coefficient.


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.