Python Arithmetic Sequence Sum - python

How do I make a code that follows this? 1⋅2+2⋅3+3⋅4+…+(n−1)⋅n
For example, if n=5, the answer is 1⋅2+2⋅3+3⋅4+4⋅5=40.
n cannot be less than or equal to two or more or equal to 1000
This is my code for now but it doesn't work.
n = int(input())
if n>= 2 and n<=1000:
sum = 0;
numbers = range(1, n+1)
for amount in numbers:
if (amount % 2 == 1):
sum *= amount
else:
sum += amount
print(sum)

For every number between 1 and n-1 (inclusive), you need to multiply it by the following number, and then sum them all. The easiest way to represent this is with a comprehension expression over a range call:
result = sum(i * (i + 1) for i in range(1, n))

You need to reproduce exactly the scheme you give
for each number, mulitply it with itself-1, and sum that
def compute(n):
if 2 <= n <= 1000:
total = 0
for amount in range(1, n + 1):
total += amount * (amount - 1)
print(total)
But that's the same as multiplying each with itself+1, if you change the bound to get one step less
for amount in range(1,n):
total += amount * (amount + 1)
Then you can use builtin methos sum and a generator syntax
def compute(n):
if 2 <= n <= 1000:
total = sum(nb * (nb + 1) for nb in range(1,n))
print(total)

If you try to approach it mathematically, you can have the answer in a single expression.
Dry run your code. You will see that for n = 5, you are doing as follows:
Num of Iterations = 6 (1 -> 5+1)
Iteration 1
sum = 0 + 1 = 1
Iteration 2
sum = 1 * 2 = 2
Iteration 3
sum = 2 + 3 = 5
Iteration 4
sum = 5 * 4 = 20
Iteration 5
sum = 20 + 5 = 25
Iteration 6
sum = 25 * 6 = 150
In this, you are completely disregarding the BODMAS/PEMDAS rule of multiplication over addition in the process of regenerating and calculating the series
What you need to do is
Iteration 1:
sum = 0 + 2 * 1 = 2
Iteration 2:
sum = 2 + 3 * 2 = 8
Iteration 3:
Sum = 8 + 4*3 = 20
Iteration 4:
Sum = 20 + 5*4 = 40
Here, We have broken the step as follows:
For each iteration, take the product of (n) and (n-1) and add it to the previous value
Also note that in the process, we are first multiplying and then adding. ( respecting BODMAS/PEMDAS rule )
So, you need to go from n = 2 to n = 5 and on each iteration you need to do (n-1)*(n)
As mentioned earlier, the loop is as follows:
## Checks for n if any
sum = 0
for i in range(2, n):
sum = sum + (i-1)*i
print(sum)

Related

Excluding a digit in a for loop

Hi I would like to exclude any number that contains a 7 from the range of 1-1000 in my for loop.
This is the code I have so far
sum = 0
for i in range(1,1001):
if #digit contains a 7:
continue
sum = sum + 1/i
return sum
Here's one way to do it by isolating each of the 3 possible digits in your range:
sum = 0
for i in range(1,1001):
if 7 not in (i-10*(i//10), i//10-10*(i//100), i//100-10*(i//1000)):
sum = sum + 1/i
print(sum)
Output:
6.484924087833117
UPDATE: To generalize for an arbitrary top number other than 1001, you could do this:
sum = 0
top = 1001
tens, temp = [1], 1001
while temp > 0:
tens.append(tens[-1] * 10)
temp //= 10
for i in range(1,top):
digits = [i // tens[j] - 10 * (i // tens[j + 1]) for j in range(len(tens) - 1)]
if 7 not in digits:
sum = sum + 1/i
print(sum)
As suggested in comments by #Lukas Schmid, this also works and may be preferable (it's certainly easier to read):
sum = 0
top = 1001
tens, temp = [1], 1001
while temp > 0:
tens.append(tens[-1] * 10)
temp //= 10
for i in range(1,top):
digits = [i // tens[j] % 10 for j in range(len(tens) - 1)]
if 7 not in digits:
sum = sum + 1/i
print(sum)
One of the simplest way to achieve that without using lots of maths, is to treat the number as a string and compare it to the char '7':
sum = 0
for i in range(1, 1000):
if '7' in str(i):
continue
sum += 1/i
return sum
Also, note that the return statment should be outside the for loop.

Optimizing the algorithm for brute force numbers in the problem

How many pairs (i, j) exist such that 1 <= i <= j <= n, j - i <= a?
'n' and 'a' input numbers.
The problem is my algorithm is too slow when increasing 'n' or 'a'.
I cannot think of a correct algorithm.
Execution time must be less than 10 seconds.
Tests:
n = 3
a = 1
Number of pairs = 5
n = 5
a = 4
Number of pairs = 15
n = 10
a = 5
Number of pairs = 45
n = 100
a = 0
Number of pairs = 100
n = 1000000
a = 333333
Number of pairs = 277778388889
n = 100000
a = 555555
Number of pairs = 5000050000
n = 1000000
a = 1000000
Number of pairs = 500000500000
n = 998999
a = 400000
Number of pairs = 319600398999
n = 898982
a = 40000
Number of pairs = 35160158982
n, a = input().split()
i = 1
j = 1
answer = 0
while True:
if n >= j:
if a >= (j-i):
answer += 1
j += 1
else:
i += 1
j = i
if j > n:
break
else:
i += 1
j = i
if j > n:
break
print(answer)
One can derive a direct formula to solve this problem.
ans = ((a+1)*a)/2 + (a+1) + (a+1)*(n-a-1)
Thus the time complexity is O(1). This is the fastest way to solve this problem.
Derivation:
The first a number can have pairs of (a+1)C2 + (a+1).
Every additional number has 'a+1' options to pair with. So, therefore, there are n-a-1 number remaining and have (a+1) options, (a+1)*(n-a-1)
Therefore the final answer is (a+1)C2 + (a+1) + (a+1)*(n-a-1) implies ((a+1)*a)/2 + (a+1) + (a+1)*(n-a-1).
You are using a quadratic algorithm but you should be able to get it to linear (or perhaps even better).
The main problem is to determine how many pairs, given i and j. It is natural to split that off into a function.
A key point is that, for i fixed, the j which go with that i are in the range i to min(n,i+a). This is since j-i <= a is equivalent to j <= i+a.
There are min(n,i+a) - i + 1 valid j for a given i. This leads to:
def count_pairs(n,a):
return sum(min(n,i+a) - i + 1 for i in range(1,n+1))
count_pairs(898982,40000) evaluates to 35160158982 in about a second. If that is still to slow, do some more mathematical analysis.
Here is an improvement:
n, a = map(int, input().split())
i = 1
j = 1
answer = 0
while True:
if n >= j <= a + i:
answer += 1
j += 1
continue
i = j = i + 1
if j > n:
break
print(answer)

complicated variable assignment

I usually do my incrementation this way:
n=0
n=n+1 # and never do n+=1
Now there is code that I am trying to comprehend and I'm struggling to understand it.
sum=0
temp = num
while temp > 0:
digit = temp % 10
# below is the line I do not understand
sum += digit ** power # what is happening here?. with power= len(str(num))
temp //= 10
if num == sum:
print num
This snippet is a piece to list the armstrong numbers.
In python ** is the sign for exponent, so x ** 2 is x^2 (or x squared).
x += g is the same as x = x + g
sum += digit ** power == sum = sum + (digit ** power)
while temp > 0:
digit = temp % 10
sum += digit ** power
temp //= 10
Take the last digit of temp
Add to sum the digit to the power of power
Delete the last digit of temp
suppose num = 34, then power becomes 2, so, for first iteration:
digit = 4
sum = 0 + 4 ** 2 # which is 0 + 16 = 16. Then adding to sum
so, digit ** power is digit to the power of 2
similarly, for second iteration:
digit = 3
sum = 16 + 3 ** 2 # which is 16 + 9 = 25

How to get sum of numbers having exactly 2 one's in their binary representation in a range?

I want to find out the sum of numbers in a range say N which has exactly 2 ones in its binary representation.I wrote the code as:
N = int(raw_input())
sum1 = 0
for i in range(1, N + 1):
if bin(i)[2:].count('1') == 2:
sum1 = sum1 + i
This code takes a lot of time. Is there any faster way to do this calculation?
Try this code:
def two_bit_numbers(N):
bit0 = 0
bit1 = 1
while True:
n = (1<<bit1) + (1<<bit0)
if n > N:
break
yield n
bit0 += 1
if bit0 == bit1:
bit1 += 1
bit0 = 0
N = 100
sum1 = 0
for i in two_bit_numbers(N):
# print i, bin(i)
sum1 += i
print sum1
If we look at how to find number that have two '1' when written in binary, we get to two cases :
We have an element > 1 with 1 '1' in binary representation and we add 1 to it
We have an element that has two '1' in binary representation, and we multiply it by 2.
So, to look for the number of element matching this case, you could just do :
num = int(raw_input())
def get_numbers(N):
sum1 = []
sum2 = []
i = 2
while i < N:
# we add the elements of case 2
sum1.extend(sum2)
# we add the element of case 1
sum1.append(i+1)
sum2 = [2*x for x in sum1 if x > i]
# we check for the elements with one more number when written in binary
i *= 2
# sum1 now contains all elements with two '1' in binary between 0 and the power of 2 above N.
# we remove the elements above N
sum1 = [x for x in sum1 if x <= N]
# we sort the list
sum1.sort()
# we take the length of the list, of the number of elements with two '1' in binary between 0 and N
return sum1
print(get_numbers(num))
This should be faster, as we do not test every number between 0 and N, and have a log2(N) complexity.
Do not hesitate if you have any question about my method.
This method is slower than the one in the other answer (around 2 times slower)

Python - maximum number of terms added of a series such that sum < 5?

Hi I was asked the following:
Consider the series
Total = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 .... + 1/N
What is the maximum number of terms added (i.e. the value of N) such that Total < 5.0?
(write a few lines of Python code stopping when the sum is just less than 5.0)
So far thats what i did:
m = 5
n= 0
u = 1
sum_serie = 0
for u in range(1,100):
u = 1/(n+1)
n= n+1
while u < m:
sum_serie = sum_serie + u
print(sum_serie)
print(n)
it doesn't work. could someone explain? is there an easier way of doing it?
thanks
This is perhaps the corrected version:
m = 5
n = 0
sum_series = 0
while sum_series < m:
u = 1. / (n + 1)
sum_series = sum_series + u
n = n + 1
print(sum_series)
print(n)
which prints:
5.00206827268
83
First your while loop should have the condition sum_serie < m and parts u = 1. / (n + 1) and n = n + 1 should be within that loop as shown. The for loop doesn't do what you need so it has been removed.
To loop to the number just below the limit, just check the limit as a loop condition (here in a while) and change the value inside the loop. Something like;
limit = 5
n = 0
sum_serie = 0
while sum_serie + 1./(n+1) < limit:
sum_serie += 1./(n+1)
n += 1
print(sum_serie)
print(n)
For your data, you get the output;
4.9900200799090815
82

Categories

Resources