2 * 2 * 2... j times without the ** operator - python

I am doing a challenge where I am supposed to calculate 2 * 2 * 2 ... j times, but without using the ** operator.
I tried using square but when I thought about it, it couldn't be squaring.
N = int(input('Num: '))
x = 1
while True:
if x * x > 0:
break
else:
x += 1
print(x - 1 * x - 1)
The result should be 32 if I put in 5 but I actually get -1.

You can bit shift:
N = int(input('Num: '))
print(1 << N)
Or, just use the pow built-in, though it's basically the ** operator:
N = int(input('Num: '))
print(pow(2, N))
If you wanted to use a loop:
N = int(input('Num: '))
result = 1
for _ in range(N):
result *= 2
print(result)

I think you are overcomplicating things:
j = int(input('Num: '))
x = 1
for _ in range(j):
x *= 2
print(x)
or
j = int(input('Num: '))
x = 1
while j > 0:
x *= 2
j -= 1
print(x)

import math
math.pow(x, N)
That should do it!
Edit: math.pow will break for large values of N as described in the comments. Use the solution of Tomothy32 instead.

Related

Sum of 1+3+5...+n until the sum exceeds 100

Then the sum and the last added number and the number of numbers added must be printed.
I am currently stuck, I managed to get the sum part working. The last added number output is printed "23" but should be "21". And lastly, how can I print the number of numbers added?
Output goal: 121, 21, 11
Here is my code:
n = int()
sum = 0
k = 1
while sum <= 100:
if k%2==1:
sum = sum + k
k = k + 2
print('Sum is:', sum)
print("last number:", k)
Edit: Would like to thank everyone for their help and answers!
Note, that (you can prove it by induction)
1 + 3 + 5 + ... + 2 * n - 1 == n**2
<----- n items ----->
So far so good in order to get n all you have to do is to compute square root:
n = sqrt(sum)
in case of 100 we can find n when sum reach 100 as
n = sqrt(100) == 10
So when n == 10 then sum == 100, when n = 11 (last item is 2 * n - 1 == 2 * 11 - 1 == 21) the sum exceeds 100: it will be
n*n == 11 ** 2 == 121
In general case
n = floor(sqrt(sum)) + 1
Code:
def solve(s):
n = round(s ** 0.5 - 0.5) + 1;
print ('Number of numbers added: ', n);
print ('Last number: ', 2 * n - 1)
print ('Sum of numbers: ', n * n)
solve(100)
We have no need in loops here and can have O(1) time and space complexity solution (please, fiddle)
More demos:
test : count : last : sum
-------------------------
99 : 10 : 19 : 100
100 : 11 : 21 : 121
101 : 11 : 21 : 121
Change your while loop so that you test and break before the top:
k=1
acc=0
while True:
if acc+k>100:
break
else:
acc+=k
k+=2
>>> k
21
>>> acc
100
And if you want the accumulator to be 121 just add k before you break:
k=1
acc=0
while True:
if acc+k>100:
acc+=k
break
else:
acc+=k
k+=2
If you have the curiosity to try a few partial sums, you immediately recognize the sequence of perfect squares. Hence, there are 11 terms and the last number is 21.
print(121, 21, 11)
More seriously:
i, s= 1, 1
while s <= 100:
i+= 2
s+= i
print(s, i, (i + 1) // 2)
Instead of
k = k + 2
say
if (sum <= 100):
k = k +2
...because that is, after all, the circumstance under which you want to add 2.
To also count the numbers, have another counter, perhasp howManyNumbers, which starts and 0 and you add 1 every time you add a number.
Just Simply Change you code to,
n = int()
sum = 0
k = 1
cnt = 0
while sum <= 100:
if k%2==1:
sum = sum + k
k = k + 2
cnt+=1
print('Sum is:', sum)
print("last number:", k-2)
print('Number of Numbers Added:', cnt)
Here, is the reason,
the counter should be starting from 0 and the answer of the last printed number should be k-2 because when the sum exceeds 100 it'll also increment the value of k by 2 and after that the loop will be falls in false condition.
You can even solve it for the general case:
def sum_n(n, k=3, s =1):
if s + k > n:
print('Sum is', s + k)
print('Last number', k)
return
sum_n(n, k + 2, s + k)
sum_n(int(input()))
You can do the following:
from itertools import count
total = 0
for i, num in enumerate(count(1, step=2)):
total += num
if total > 100:
break
print('Sum is:', total)
print("last number:", 2*i + 1)
To avoid the update on k, you can also use the follwoing idiom
while True:
total += k # do not shadow built-in sum
if total > 100:
break
Or in Python >= 3.8:
while (total := total + k) <= 100:
k += 2
Based on your code, this would achieve your goal:
n = 0
summed = 0
k = 1
while summed <= 100:
n += 1
summed = summed + k
if summed <= 100:
k = k + 2
print(f"Sum is: {summed}")
print(f"Last number: {k}")
print(f"Loop count: {n}")
This will solve your problem without changing your code too much:
n = int()
counter_sum = 0
counter = 0
k = 1
while counter_sum <= 100:
k+= 2
counter_sum =counter_sum+ k
counter+=1
print('Sum is:', counter_sum)
print("last number:", k)
print("number of numbers added:", counter)
You don't need a loop for this. The sum of 1...n with step size k is given by
s = ((n - 1) / k + 1) * (n + 1) / k
You can simplify this into a standard quadratic
s = (n**2 - k * n + k - 1) / k**2
To find integer solution for s >= x, solve s = x and take the ceiling of the result. Apply the quadratic formula to
n**2 - k * n + k - 1 = k**2 * x
The result is
n = 0.5 * (k + sqrt(k**2 - 4 * (k - k**2 * x - 1)))
For k = 2, x = 100 you get:
>>> from math import ceil, sqrt
>>> k = 2
>>> x = 100
>>> n = 0.5 * (k + sqrt(k**2 - 4 * (k - k**2 * x - 1)))
>>> ceil(n)
21
The only complication arises when you get n == ceil(n), since you actually want s > x. In that case, you can test:
c = ceil(n)
if n == c:
c += 1

Pythagorean triple with python

I want to get a number 'n' and produce Pythagorean triple that total of them is equal with 'n'.
for example for n=12 my output is 3, 4, 5 (12 = 3 + 4 + 5).
I write below code but it take a lot of time for big numbers. please help me to improve it.
a = int(input())
done = False
for i in range(int(a/4)+1,2,-1):
if done:
break
for j in range(i+1,int(a/2)+1):
k = a-(i+j)
if k <= j:
break
if i**2 + j**2 == k**2:
print(i,j,k)
done = True
break
if done == False:
print('Impossible')
This code may help you
limits = int(input())
c, m = 0, 2
# Limiting c would limit
# all a, b and c
while c < limits :
# Now loop on n from 1 to m-1
for n in range(1, m) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
# if c is greater than
# limit then break it
if c > limits :
break
if a+b+c == limits:
print(a, b, c)
m = m + 1
>> 12
>> 3 4 5
I've used the joblib module to parallelize your code, though I haven't tested if there is a speedup for very large n; let me know:
from joblib import Parallel, delayed
done = False
def triple(a):
global done
for i in range(int(a/4)+1,2,-1):
if done:
break
for j in range(i+1,int(a/2)+1):
k = a-(i+j)
if k <= j:
break
if i**2 + j**2 == k**2:
print(i,j,k)
done = True
break
if done == False:
print('Impossible')
if __name__ == '__main__':
a = int(input("n:"))
Parallel(n_jobs=-1, backend="threading")(map(delayed(triple), [a]))
To generate a Pythagorean triplet of a given sum, you can run two loops, where the first loop runs from i = 1 to n/3, the second loop runs from j = i+1 to n/2. In second loop, we check if (n – i – j) is equal to i * i + j * j.
n = int(input()
for i in range(1, int(n / 3) + 1):
for j in range(i + 1, int(n / 2) + 1):
k = n - i - j
if (i * i + j * j == k * k):
print(i, j, k)

Python function returning first value twice

I've written this function to calculate sin(x) using Taylor series to any specified degree of accuracy, 'N terms', my problem is the results aren't being returned as expected and I can't figure out why, any help would be appreciated.
What is am expecting is:
1 6.28318530718
2 -35.0585169332
3 46.5467323429
4 -30.1591274102
5 11.8995665347
6 -3.19507604213
7 0.624876542716
8 -0.0932457590621
9 0.0109834031461
What I am getting is:
1 None
2 6.28318530718
3 -35.0585169332
4 46.5467323429
5 -30.1591274102
6 11.8995665347
7 -3.19507604213
8 0.624876542716
9 -0.0932457590621
Thanks in advance.
def factorial(x):
if x <= 1:
return 1
else:
return x * factorial(x-1)
def sinNterms(x, N):
x = float(x)
while N >1:
result = x
for i in range(2, N):
power = ((2 * i)-1)
sign = 1
if i % 2 == 0:
sign = -1
else:
sign = 1
result = result + (((x ** power)*sign) / factorial(power))
return result
pi = 3.141592653589793
for i in range(1,10):
print i, sinNterms(2*pi, i)
I see that you are putting the return under the for which will break it out of the while loop. You should explain if this is what you mean to do. However, given the for i in range(1,10): means that you will ignore the first entry and return None when the input argument i is 1. Is this really what you wanted? Also, since you always exit after the calculation, you should not do a while N > 1 but use if N > 1 to avoid infinite recursion.
The reason why your results are off is because you are using range incorrectly. range(2, N) gives you a list of numbers from 2 to N-1. Thus range(2, 2) gives you an empty list.
You should calculate the range(2, N+1)
def sinNterms(x, N):
x = float(x)
while N >1:
result = x
for i in range(2, N):
Your comment explains that you have the lines of code in the wrong order. You should have
def sinNterms(x, N):
x = float(x)
result = x
# replace the while with an if since you do not need a loop
# Otherwise you would get an infinite recursion
if N > 1:
for i in range(2, N+1):
power = ((2 * i)-1)
sign = 1
if i % 2 == 0:
sign = -1
# The else is not needed as this is the default
# else:
# sign = 1
# use += operator for the calculation
result += (((x ** power)*sign) / factorial(power))
# Now return the value with the indentation under the if N > 1
return result
Note that in order to handle things set factorial to return a float not an int.
An alternative method that saves some calculations is
def sinNterms(x, N):
x = float(x)
lim = 1e-12
result = 0
sign = 1
# This range gives the odd numbers, saves calculation.
for i in range(1, 2*(N+1), 2):
# use += operator for the calculation
temp = ((x ** i)*sign) / factorial(i)
if fabs(temp) < lim:
break
result += temp
sign *= -1
return result

Syntax - Assigning a variable then putting it in while loop doesn't work. Why?

I'm wondering why this won't work:
n = 10
x = 1
while x < n:
x += 1
n += n * (x - 1)
print n
I've already assigned n = 10, placing it in the while loop should be like placing a 10 there. Running it in Terminal acts like I've placed a raw_input() there.
This works:
n = 10
x = 1
while x < 10:
x += 1
n += n * (x - 1)
print n
It looks the same to me...
Thanks!
In the first example, n, your limit, is increasing (n += n * (x - 1)) and you have an infinite loop. In the second one the limit is constant (10) so it terminates.
Because, in the while loop, for each iteration, you also update n
while x < n:
x += 1
n += n * (x - 1)
So, after the first few iterations, n is no longer 10, and it keeps increasing.
I finally understand it after doing more loops of similar type. The reason is that this creates an infinite loop because in:
n = 10
x = 1
while x < n:
x += 1
n += n * (x - 1)
print n
The n increases along with x:
x += 1
n += n * (x - 1)
So x never catches up with n and causes an infinite loop.
This works because the 10 never increases and x can catch up with it.
n = 10
x = 1
while x < 10:
x += 1
n += n * (x - 1)
print n

Leaving the for / while loop if certain conditions are met

I wrote a set of codes to calculate a Diophantine equation:
bestSoFar = 0
packages = (6,9,20)
numMc = 0
guess= 0
possibn = []
for n in xrange(1, 150):
for a in xrange(0, (n/ packages[0]) +1):
for b in xrange(0,(n/ packages[1]) +1):
c = (n - packages[0]* a - b * packages[1]) / packages[-1]
numMc = packages[0] *a + packages[1] * b + packages[-1] * c
if numMc == n and n not in possibn:
possibn.append(n)
print possibn
if len(possibn) >6 and possibn [-1] - possibn[-6] == 5:
bestSoFar = n
break
The original problem set is designed by the MIT course. Basically it is to calculate the number of McNuggets that could be bought by arranging the ratio of packages-in-different-size ( McDonald does 6,9,20 McNuggets in a package). Say, 21 McNuggets could be bought by buying two 6-McNuggets and one 9-McNuggets. if the number of McNuggets are possible to be bought in exact quantity of packages, I store them into a list. It is found that if 6 consecutive numbers are also possible to be bought in exact quantity, the remained numbers could also be possible.
From my code the result of bestSoFar=149 while the expected answer is 40. The reason would be that it keeps looping until n reaches 149. I would like to stop right at 40 ( with the break statement). However, it fails and I am seeking the advices for you all. Also, if there is anyway to program the problem faster/easier, I am happy to know and learn it too.
Thank you so much.
Casey
If you are not supposed to use a function, just assign a variable to cause breaks out of the other loops.
bestSoFar = 0
packages = (6,9,20)
numMc = 0
guess= 0
possibn = []
finished = False
for n in xrange(1, 150):
for a in xrange(0, (n/ packages[0]) +1):
for b in xrange(0,(n/ packages[1]) +1):
c = (n - packages[0]* a - b * packages[1]) / packages[-1]
numMc = packages[0] *a + packages[1] * b + packages[-1] * c
if numMc == n and n not in possibn:
possibn.append(n)
# print possibn
if len(possibn) >6 and possibn [-1] - possibn[-6] == 5:
bestSoFar = n
finished = True
break
if finished: break
print bestSoFar
Turn it into a function and return:
from __future__ import print_function
def solve(*packages):
bestSoFar = 0
numMc = 0
guess= 0
possibn = []
for n in xrange(1, 150):
for a in xrange(0, (n / packages[0]) + 1):
for b in xrange(0, (n / packages[1]) + 1):
c = (n - packages[0] * a - b * packages[1]) / packages[-1]
numMc = packages[0] * a + packages[1] * b + packages[-1] * c
if numMc == n and n not in possibn:
possibn.append(n)
print possibn
if len(possibn) > 6 and possibn [-1] - possibn[-6] == 5:
return n
return bestSoFar
x = solve(6, 9, 20)
print(x)
I am not actually clear on what you are expecting. But, what i see is you want to break out of everything. The break which you have given only exits inner loop. Put another break statement outside the inner loop and inside first loop.

Categories