Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I am totally new for python3. I need to write some easy process as following. But wondering if there is anyway I can simplify the following code?
def dosomething ( i ):
print(i);
n = 12
i = 1
while n > 0:
if i == 6:
i = 5
dosomething( i )
i += 1
n -= 1
It's not clear what you're trying to do with this example or if your constants are significant, but here's one approach that creates the same output a bit more simply:
def dosomething ( i ):
print(i);
n = 12
i = 1
for j in range(1, 6):
dosomething(j)
for k in range(n - 5):
dosomething(5)
In general, if the number of iterations is known in advance, you should express it with a for loop rather than a while loop.
this would work, simplifying the while loop into a for loop:
i = 1
for n in range(1, 13):
if i == 6:
i = 5
dosomething(i)
i += 1
if you are trying to avoid dosomething(6):
for n in range(1, 13):
if n != 6:
dosomething(n)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I tried to do a function that builds the Fibonacci series but when I try to check the calculation comes out wrong
def fibo(n):
i=1
j=1
for n in range(1,n):
j=j+i
i=j+i
return i+j
n=input('Enter number:')
print(fibo(int(n)))
i, j = 1, 1
for _ in range(n):
j = j + i
i = j + i
This is not the fibonacci sequence. Instead, try:
i, j = 1, 1
for _ in range(n):
i, j = j, i+j
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I already got the following part and I think its a fast and clear way and I dont need it to be faster:
for num in range(2,1000):
if all(num%i!=0 for i in range(2,num)):
print num
But how can I find the max difference of all the prime numbers I got and put that as the final result?
You can proceed like that:
biggest_prime = 2
for num in range(2,1000):
if all(num%i!=0 for i in range(2,num)):
biggest_prime = num
print "Difference: " + str(biggest_prime - 2) # outputs 995
Basically, you just store the last found prime, and when you're out of the loop you will have the value of the highest prime below 1000. You can then substract 2 to it.
If you don't like the fact that the 2is hard-coded, you can add a second variable lowest_prime that will change value only once.
Don't output each prime; store it in a list, so that once you have the full list, you can do what you like with the primes.
primes = []
for num in range(2, 1000):
if all(num % i != 0 for i in range(2, num)):
primes.append(num)
More briefly,
primes = [num for num in range(2, 1000) if all(num % i != 0 for i in range(2, num)]
primes = []
for num in range(2, 1000):
if all(num % i != 0 for i in range(2, num)):
primes.append(num)
x = primes[-1] - primes[0]
print(x)
so is this okay?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is what I'm assigned to do:
I don't understand how to do the second half (Perfect number) of the rubric.
This is what I have so far:
def sumMultiple(num):
sum = 0
for i in range(1, num//2+1):
if (num % i == 0):
sum += i
return sum
for in in range(1, 100000):
if(sumMultiple(i) == i):
print(i)
You can use a global variable that you iterate in the inner loop.
iterationCount = 0
def sumMultiple(num):
global iterationCount
sum = 0
for i in range(1, num//2+1):
if num % i == 0:
sum += i
iterationCount++
return sum
for i in range(1, 1000):
if sumMultiple(i) == i:
print(i)
print("Total iterations ", iterationCount)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the sum of all prime numbers between 1,000,000,000,000 and 1,000,000,100,000?
this works out but is very slow.I need to optimize it.I am new to python.
3614000181007876 is the right answer
A=10 ** 6
N=A+1
B=10 ** 5
prime=[]
sum=0
for i in range(0,N):
prime.append(0)
for i in range(2,N):
if(prime[i]==1):
continue
for j in range(i*i,N,i):
prime[j]=1
for i in range((A ** 2)+1,(A ** 2)+B,2):
for j in range(2,A):
c=0
if(prime[j]==1):
continue
if(i%j==0):
c=c+1
if(c>0):
break
if(c==0):
#print(i)
sum=sum+i
print(sum)
Not the most efficient way, but gets the right result (hopefully 3614000181007876) in 2 seconds on my box:
def isPrime(n):
d = n - 1
s = 0
while not d & 1:
s += 1
d >>= 1
for a in (2, 13, 23, 1662803):
if pow(a, d, n) != 1 and all(pow(a, (1 << r) * d, n) != n - 1 for r in range(0, s)):
return False
return True
print(sum(x for x in range(1000000000001, 1000000100000, 2) if isPrime(x)))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have the code
i = 0
while i < N:
print("Hello")
i += 1
how many times will Hello be printed out? (assume that N is a defined integer)
Answers:
0
N
N-1
N+1
more than N+1
and why? I never get this so I would appreciate of somebody could explain.
The best way to figure it out is to go through it by hand for a few manageable values of N. For instance, if N is 2:
i == 0 and 0 < 2 → print "hello", increment i
i == 1 and 1 < 2 → print "hello", increment i
i == 2 and 2 < 2 → while-loop condition is no longer satisfied → loop ends
So for N = 2, "hello" is printed 2 times. See the pattern?
Hello will be printed out N times.
assume N is 3.
1st iteration
i = 0
i is less than N
print hello
i = i + 1; // i = 1
2nd iteration
i = 1;
i` is less thanN (3)`
print hello
i = i + 1; // i = 2
3rd iteration
i = 2;
i is less than N (3)
print hello
i = i + 1; // i = 3
4th iteration
i = 3;
i is equal to N (3)
break loop
As the other answers described, it would print N times, because it starts at 0 and goes until it is just before N, not equal to N.
In reality, though, this is very redundant in Python. A much easier way to do this, making it a bit more readable (and hopefully easier for you to understand):
N=3
for x in range(0,N):
print "This is loop %d" % (x)
This loop will print from 0 to N, which is really just N amount of times.