This question already has an answer here:
Running time/time complexity for while loop with square root
(1 answer)
Closed 4 years ago.
i=n
while i>1 :
i = int(math.sqrt(i))
print('*')
why is the time complixity of the code above log(log(n)) ??
(Assume sqrt() runs in O(1).)
Assume n can be coded with k bits.
After first iteration, i=√n and i can be coded on k/2 bits.
Hence the number of bits required to code i is divided by 2 at every step and the number of steps is log(k).
As k=log(n), complexity is log(log(n)), if we assume that sqrt complexity is one.
Related
This question already has answers here:
Generating a list of random numbers, summing to 1
(12 answers)
Generate random numbers summing to a predefined value
(7 answers)
Getting N random numbers whose sum is M
(9 answers)
Closed 10 months ago.
Let's say I have a number m (it will be integer, usually 1) and I want to split m into n random numbers, which have the sum m. I want to make a distribution which will be random, but will be close (doesn't have to be really close) to uniform distribution.
I have this
pieces = []
for i in range(n-1):
pieces.append(random.uniform(0,m-sum(pieces)))
pieces.append(m-sum(pieces))
The problem is, that the first number is usually really high compared to the rest of the numbers. When I tried it for m = 1 and let's say n=10, the first number was close to 0,5 and the last number was close to like 0,001 (or something like that), which is not a problem, but the problem is, that the first number is on average really big compared to all of the other numbers.
My question is: Is there some kind of way to do this, so the numbers are closer? There can be a big difference between biggest and smallest number, but I want it to feel more uniform, if that makes sense.
This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 2 years ago.
I am having a dispute with a co-worker regarding a Big O question. For example, consider the following Python for-loop that prints every 100th element:
n = 10000
for i, x in range(0, n, 100):
print(i)
I think the complexity is O(log n) as opposed to O(n), because it grows not quite linearly. O(n) is not wrong, I think, but isn't O(log n) more precise?
Thanks!
First of all, if it is actually O(log n), then O(n) is in fact wrong, not just imprecise. Also, it is O(n), since it does scale linearly. Since you step by 100, you have n/100 iterations of the loop, which is linear with respect to n.
This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 6 years ago.
I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67-->70 and 64-->70. Here is my code for rounding so far:
##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)
I would appreciate it if you could answer simple and explain how it works.
One way of rounding up would be to use integer division to go down to the precision you want and then multiplying back up. e.g.,:
Sumrounded = SumOfusrinput // (-10) * (-10)
This question already has answers here:
Function for factorial in Python
(10 answers)
Closed 6 years ago.
So these are the instructions:
"Given a number “n,” if I were to multiply all positive integers up to and including
“n,” I would get a product. This product is called the factorial of “n,” which is
denoted n! (read as n factorial). It is also given that 0! = 1. Your task is to write a
Python script that implements the factorial of a number “n.” For example if “n” is
initialized with the value 5, then your script should print out the following.
The factorial of 5 is 120"
I'm stuck on how exactly to structure the while and for loop along with the range variable. Can any one help me here. It doesn't have to be the exact answer. Just a clue or something please :D
I hope this is not a homework question...
Here is pseudocode:
Create a variable to store your answer, and initialize it as 1
Loop through the numbers from 1 to n inclusive, multiplying your answer
variable by each number
Print out the answer in the desired format
Thats all!
This simple for loop works:
prod = 1 # solution variable
for i in range(1, n+1):
prod *= i # do the work, i.e., make the solution
print("The factorial of %s is %s" % (n, prod))
We don't need to check for zero as the loop is skipped if so.
Hope this helps!
(Alternatively, you could use math.factorial(n), but you aren't learning anything if you use builtins)
Tell me if you have more questions.
This question already has answers here:
Speed of calculating powers (in python)
(6 answers)
Closed 7 years ago.
Read somewhere in my adventure in optimizing my mandelbrot program that if you are for example squaring something. Using the mandelbrot equation as the example that
z * z is faster than z**2. Is this true, and if it is why is this so? Is it due to the fact that z**2 still computes trivial cases such as 0?
I am one of the people who have suggested the optimization and yes, it is true. In the example below, squaring with * is over 3 times as fast as with **2.
from timeit import repeat
print(repeat('z*z', 'z=.54321 + .56789j'))
print(repeat('z**2', 'z=.54321 + .56789j'))
# prints
[0.07780417092306088, 0.05484807785182001, 0.05577646621573226]
[0.2081317598277747, 0.19060335713839965, 0.1913974058615736]
Since both operations give the same answer
>>> z*z
(-0.02742194800000003+0.6169670537999999j)
>>> z**2
(-0.02742194800000003+0.6169670537999999j)
the second must eventually do the first (plus whatever else it does.