Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11190

Python counting probabilites of more than one item

I wanted to construct a small program in python, counting the probabilities of a random number being divisible by a number.

In C, i would create an array, with the numbers and loop through them, adding one to another array that would store the sum of these probabilities.

I tried to do that in python with the tuples, but i cannot change their value. So what is the easiest way to do this?

Here is the code:

primes = (2,3,5,7,11,13,17,19,23,29)
numbers =(0,0,0,0,0 ,0 ,0 ,0 ,0 ,0)

for number in range(2,10000):
    for div in primes:
        x = 0
        if(number % div == 0):
            numbers[x]  += 1
        x+=1



print(numbers)

Upvotes: 2

Views: 139

Answers (2)

algorowara
algorowara

Reputation: 1720

As a proof to rich.okelly's statement, consider an infinite set of numbers. Every number that is divisible by some integer N is a multiple of N: 1N, 2N, 3N...etc. Every Nth number - that is, 1/N numbers in every set of N consecutive numbers - is divisible by N. This holds true for the entire infinite set; 1/N of all integers are divisible by N. Therefore, a random number chosen with bounds which are multiples of N (if you have, say random numbers from 1 to 15, fewer than 1/10 of them will be divisible by 10) will have a 1/N probability of being divisible by N.

Thought that might help in case you wanted more than just an assertion of the principle.

Upvotes: 1

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

The probability of a random integer being divisible by an integer n is 1/n.

Upvotes: 6

Related Questions