why using variable and a number in loop show different outcomes - python-3.x

a = 1
for number in range(5):
a = a + number * 5
print(a)
#
for number in range(5):
a = 1 + number * 5
print(a)

In the first loop, you retain the value of a, and add to it number * 5 in each iteration.
In the second loop, you always take 1 and add to it number * 5.
You can, of course, achieve the same behavior with variables, you just need to use another one:
first = 1
for number in range(5):
a = first + number * 5
print(a)

Related

Calculate The Total Result of The Arithmetic Sequence With Big Number in Less Than 1 Second

How to Construct a Python 3 function sum(n) that takes a positive integer n as an input and perform the following computation:
sum(n)=5+10+⋯+5(n−1)+5n.
The value of 𝑛n is between 1 and 10^15. The timelimit for the computation is 1 second. To make your code efficient, try to use the explicit formula (closed form) of sum(n).
Test:
print(sum(1))
print(sum(2))
print(sum(3))
Result:
5
15
30
What I Have Tried:
def sum(n):
AK = 0
n += 1
for i in range(1,n):
P = 5 * i
AK += P
return AK
Unfortunately it takes more than 1 second to finish
as Hans Kesting said, the result is 5 times the sum of 1...n and so you can try this simple and easy piece of code. I haven't actually tried it but in practice, it should be less than one second
def sum(n):
return 5 * (n * (n + 1) // 2)

I want to improve speed of my algorithm with multiple rows input. Python. Find average of consequitive elements in list

I need to find average of consecutive elements from list.
At first I am given lenght of list,
then list with numbers,
then am given how many test i need to perform(several rows with inputs),
then I am given several inputs to perform tests(and need to print as many rows with results)
every row for test consist of start and end element in list.
My algorithm:
nu = int(input()) # At first I am given lenght of list
numbers = input().split() # then list with numbers
num = input() # number of rows with inputs
k =[float(i) for i in numbers] # given that numbers in list are of float type
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
print(round(sum(k[int(a):(int(b)+1)])/(-int(a)+int(b)+1),6)) # round up to 6 decimals
But it's not fast enough.I was told it;s better to get rid of "while" but I don't know how. Appreciate any help.
Example:
Input:
8 - len(list)
79.02 36.68 79.83 76.00 95.48 48.84 49.95 91.91 - list
10 - number of test
0 0 - a1,b1
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 7
2 7
Output:
79.020000
57.850000
65.176667
67.882500
73.402000
69.308333
66.542857
69.713750
68.384286
73.668333
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
Replace your while-loop with a for loop. Also you could get rid of multiple int calls in the print statement:
for _ in range(int(num)):
a, b = [int(j) for j in input().split()]
You didn't spell out the constraints, but I am guessing that the ranges to be averaged could be quite large. Computing sum(k[int(a):(int(b)+1)]) may take a while.
However, if you precompute partial sums of the input list, each query can be answered in a constant time (sum of numbers in the range is a difference of corresponding partial sums).

For loop multiply in some range

I have a task for my college in which I must program some very simple logic for multiplying numbers in range like 1 to 5 and than multiply like 1*2*3*4*5. And this way for any input number. For 7 it would be 1*2*3*4*5*6*7.
This is my modest code which is not finished because of no idea how to do it .Help, please..
number = int(input("Enter a number:"))
number += 1
for i in range(1,number):
a = i*(number*
print(a)
Try this:
In [1774]: number = int(input("Enter a number:"))
In [1775]: a = 1
In [1776]: for i in range(1, number+1):
...: a *= i
In [1781]: a
Out[1781]: 120
a's value is 120 which is basically (1*2*3*4*5). Hope this helps.

Different output in for loop

I made a simple program to generate prime numbers but It shows 10 as a prime number. In debugging the for loop incremented i from 2 to 4 in single step when number went from 9 to 10.
for i in range (2, int( number / 2) + 1):
if number % i == 0:
number += 1
i = 2
yield number
number += 1`

number of executions in an algorithm

For this algorithm:
i = 1
while(i<=2n){
x = x + 1
i = i + 2
}
can someone tell me how to find the formula for the number of times x = x + 1 is executed?
i goes from 1 to 2n (inclusive), so first thought is 2n.
But we see that i increments by two at a time instead of one, so it's half of that: n.
With n<1 number of times executed=0.
With n>=1 number of times executed=n.

Resources