I am pretty new to python so i dont know many things.
So lets say i got this piece of code
for i in range(10000):
n = random.randrange(1, 1000**20)
ar.append(n)
print(n)
The smallest number i get out of the 10000 is always 3-4 digits smaller than 1000^20
in this particular example the smallest numbers are
2428677832187681074801010996569419611537028033553060588
8134740394131686305349870676240657821649938786653774568
44697837467174999512466010709212980674765975179401904173
50027463313770628365294401742988068945162971582587075902
61592865033437278850861444506927272718324998780980391309
71066003554034577228599139472327583180914625767704664425
90125804190638177118373078542499530049455096499902511032
100371114393629537113796511778272151802547292271926376588
i have tried multiple ranges on multiple occasions and every time i get the same results.Maybe i am doing something wrong?
Okay, so let's use small numbers to illustrate that clearly.
Suppose you pick a random number from 1 to 100, inclusively.
How many 1 digit numbers are there? 9, from 1 to 9. For 2 digits it's from 10 to 99, 90 numbers. For 3 digits it's just 1, 100. Since it's a random pick, the probability of picking one of those 100 (9 + 90 + 9) numbers is equal, and it should be intuitively clear it's 1/100. But since there are 90 2-digit numbers, the probability of picking a 2-digit number is 90/100, or 9/10. For 1-digit numbers it's 9/100, and for the only 3-digit one it's 1/100.
I hope that answers the question.
Related
so I was given a problem where I have to write a program that the user has to give a positive integer number N and it has to print all numbers up to N but the multiples of 2 must be turned to EPL and multiples of 9 into 032 and for both multiples of 2 and 9 to give EPL 032. I have trouble with the loops as I don't know how to put one into the other.
So I stumbled upon this problem on Super Natural Numbers on HackerEarth, the problem statement goes something like this -
You are given a number n.
A supernatural number is a number whose product of digits is equal to n, and in this number, there is no digit 1.
Count the number of supernatural numbers for a given n.
Input
Contains a single integer n, 1 <= n <= 100.
Output
Print the number of supernatural numbers.
I need someone to brief me on this concept of Super Natural Number and how can I develop a code for it in Python3
My Logic - So, as I come to understand, Super Natural Numbers are numbers that are equal to the product of two digits of another number. For example - Let's take 12, so 12 is equal to the product of 3 and 4 as in 34 or 43, or 2 and 6 as in 26 or 62.
Please correct me if I'm wrong.
The logic for Code - Let me take two lists and have all digits from 1 to 10 as their elements, If I multiply these two lists element-wise, I'll have a list of 100 elements. Then I can search that list for my initial input N and use a counter to find the number of super natural numbers.
I like your logic, but I think you'll miss some.
E.g. 32 = 2x2x2x2x2, so the supernatural numbers of 32 would be 48, 84, 442, 424, 244, 2224, 2242, 2422, 4222 and 22222.
In your logic, you would only find 48 and 84.
I'm stuck in Codewars Kata, I hope someone could help me out (without spoiling the solution).
In fact the problem is that I didn't fully understand how it should work, I got the idea of the exercise but things are a bit confusing especially in Sample tests.
Here are the instructions:
The number 81 has a special property, a certain power of the sum of its digits is equal to 81 (nine squared). Eighty one (81), is the first number in having this property (not considering numbers of one digit). The next one, is 512. Let's see both cases with the details.
8 + 1 = 9 and 9^2 = 81
512 = 5 + 1 + 2 = 8 and 8^3 = 512
We need to make a function, power_sumDigTerm(), that receives a number n and may output the nth term of this sequence of numbers. The cases we presented above means that:
power_sumDigTerm(1) == 81
power_sumDigTerm(2) == 512
And here are the sample tests:
test.describe("Example Tests")
test.it("n = " + str(1))
test.assert_equals(power_sumDigTerm(1), 81)
test.it("n = " + str(2))
test.assert_equals(power_sumDigTerm(2), 512)
test.it("n = " + str(3))
test.assert_equals(power_sumDigTerm(3), 2401)
test.it("n = " + str(4))
test.assert_equals(power_sumDigTerm(4), 4913)
test.it("n = " + str(5))
test.assert_equals(power_sumDigTerm(5), 5832)
My main problem is how did they get the results for the sample tests.
A good speed up trick is to not check all numbers, Any such number must be of the form a^b for integers a and b. If you find a way to enumerate those and check them you will have a fairly efficient solution.
On f(5), the sum of the numbers is 5+8+3+2 = 18. And 18^3=5832.
Brute force method would look like this for the next one:
Start at 5833, add the digits, check the powers of the sum to see if you get number. This would actually be very fast as you can see that last one only got to ^3. As soon as the power is larger than the number you are seeking, move on to the next number: 5834. When you find one, insert into a table to remember it.
The number theory experts may be able to find a more efficient method but this brute force method is likely to be pretty fast.
Grab a prime generator; you need only prime powers to generate the sequence (although you will have all integers >= 2 in the test for inclusion). This is because if a number is a composite power, it's also a prime power.
Maintain a list of current powers, indexed by the base integer. For instance, once you've made it to a limit of 100, you'll have the list
[0, 0, 64, 81, 64, 25, 36, 49, 64, 81, 100]
// Highest power no greater than the current limit
... and the current list of target numbers has only one element: [81]
Extending the limit:
Pick the lowest number in the list (25 = 5^2, in this case)
Multiply by its base: 25 => 125
Check: is 1+2+5 a root of 125? (There are minor ways to speed this up)
If so, add 125 to the list
Now, go back to all lower integers (the range [2, 5-1] ), and add any smaller prime powers of those integers. (I haven't worked out whether there can ever be more than one power to add for a given integer; that's an interesting prime-based problem.)
Whenever you add a new target number, make sure you insert in numerical order; the step in the previous paragraph may introduce a "hit" lower than the one that triggered the iteration. For instance, this could append 5832 before in finds 4913 (I haven't coded and executed this algorithm). You could collect all the added target numbers, sort that list, and append them as a block.
Although complicated, I believe that this will be notably faster than the brute-force methods given elsewhere.
i want to make a program to find how many number is divisible by 3 or 5 in a number for example 10 has 3 9 6 divisible by 3 and has 5 and 10 divisible by 5 so total is 5 and so on so i write my code
import math
n=float(raw_input())
div3=(n-2)/3
div5=(n-4)/5
f1=math.ceil(div3)
f2=math.ceil(div5)
sumss=f1+f2
print int(sumss)
but in some number it get wrong answer and the range of input number will be
from 1 to 10^18 so i need to use math in it because the time limit for the problem test is 2 second any one have any efficiently equation to make that the loop cant make it it take very long time
This is probably a project Euler question. The issue is that some numbers can be shared by 3 and 5. For instance 22:
divisors of 3: 3 6, 9, 12, 15, 18, 21.
divisors of 5: 5 10, 15, 20
For both 15 occurs, so you did a double count.
The advantage is that 3 and 5 are relatively prime, so the only numbers that are shared are the ones dividable by 15. So you simply need to undo the double counting:
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=div3+div5-div15
print sumss
In case you allow double counting (15 should be counted twice), you can simply use:
n=int(raw_input())
div3=n//3
div5=n//5
sumss=div3+div5
print sumss
Note that the programs omitted the floating point arithmetic: this will result in both faster and more precise programs since floating point numbers work with a limited mantisse and thus can fail to represent a large number correctly (resulting by small errors). Furthermore in general integer arithmetic is faster.
Project Euler #1
Now the problem statement of Project Euler is a bit different: it asks to sum up these numbers. In order to do that, you have to construct an expression to sum up the first k multiples of l:
k
---
\
/ l*i
---
i=1
Using Wolfram Alpha, one gets this expression. So you can calculate these as:
def suml (k,l) :
return k*(k+1)*l/2
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=suml(div3,3)+suml(div5,5)-suml(div15,15)
print sumss
This program gives 119 for n=22 which - you can verify above - is correct if you count 15 only once.
I am not sure whether I got the question right, but here is some idea:
n=float(raw_input())
div3=int(n/3)
div5=int(n/5)
div15=int(n/15)
sumss=div3+div5-div15
print sumss
EDIT: Ah, found the project Euler.
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
That is a different task then the question posted here. It says bellow the number and to find the sum.
I am not sure whether it would be the right thing to post the solution here, so I am rather not doing that.
EDIT2: from Project Euler:
We hope that you enjoyed solving this problem. Please do not deprive
others of going through the same process by publishing your solution
outside Project Euler. If you want to share your insights then please
go to thread 1 in the discussion forum.
I was running a procedure to be like one of those games were people try to guess a number between 0 and 100 where there are 100 people guessing.I then averaged how many different guesses there are.
import random
def averager(times):
tests=[]
for i in range(times):
l=[]
for i in range(0,100):
l.append(random.randint(0,100))
tests.append(len(set(l)))
return (sum(tests))/len(tests)
print(averager(1000))
For some reason, the number of different guesses averages out to 63.6
Why is this?Is it due to a flaw in the python random library?
In a scenario where people were guessing a number between 1 and 10
The first person has a 100% chance to guess a previously unguessed number
The second person has a 90% chance to guess a previously unguessed number
The third person has a 80% chance to guess a previously unguessed number
and so on...
The average chance of guessing a new number(by my reasoning) is 55%.
But the data doesn't reflect this.
Your code is for finding the average number of unique guesses made by 100 people each guessing a number from 1 to 100.
As for why it converges to a number around 63... you should post your question to the math Stack Exchange.
If this was a completely flat distribution, you would expect the average to come out as 100, meaning everybody's guess was different. However, you know that such a scenario is much less random than a scenario where you have duplication. The fact that you get repeated numbers during a random sequence should be comforting.
All you are doing here is measuring some kind of uniqueness within very small sets: ie 1000 repeats of an experiment involving 100 random values. You might get a better appreciation of this if you use some sort of bootstrapping algorithm to sample from.
Also, if you scale up the number of repeats to millions, and perhaps measure the sample distribution (not just the mean), you'll have a little more confidence in the results you're getting.
It may be that the pseudo-random generator has a characteristic which yields approximately 60-70% non-repeated values inside a sequence the same length as the range. However, you would need to experiment with far more samples, as well as different random seeds. Otherwise your results are meaningless.
I modified your code so it would take an already generated sequence as input, rather than calculating random numbers:
def averager(seqs):
tests = []
for s in seqs:
tests.append(len(set(s)))
return float(sum(tests))/len(tests)
Then I made a function to return all possible choices for any given number of people and guess range:
def combos(n, limit):
return itertools.product(*((range(limit),) * n))
(One of the things I love about Python is that it's so easy to break apart a function into trivial pieces.)
Then I started testing with increasing numbers:
for n in range(2,100):
x = averager(combos(n, n))
print n, x, x/n
2 1.5 0.75
3 2.11111111111 0.703703703704
4 2.734375 0.68359375
5 3.3616 0.67232
6 3.99061213992 0.66510202332
7 4.62058326038 0.660083322911
8 5.25112867355 0.656391084194
This algorithm has a horrible complexity, so at this point I got a MemoryError. As you can see, the percentage of unique results keeps dropping as the number of people and guess range keeps increasing.
Repeating the test with random numbers:
def rands(repeats, n, limit):
for i in range(repeats):
yield [random.randint(0, limit) for j in range(n)]
for n in range(10, 101, 10):
x = averager(rands(10000, n, n))
print n, x, x/n
10 6.7752 0.67752
20 13.0751 0.653755
30 19.4131 0.647103333333
40 25.7309 0.6432725
50 32.0471 0.640942
60 38.3333 0.638888333333
70 44.6882 0.638402857143
80 50.948 0.63685
90 57.3525 0.63725
100 63.6322 0.636322
As you can see the results are consistent with what we saw earlier and with your own observation. I'm sure a bit of combinatorial math could explain it all.