Here is my code in Python:
def my_sqrt(a):
x=1
while True:
y=(x+a/x)/2.0
if y==x:
break
x=y
return y
Output I get:
>>> my_sqrt(1.0)
>>> my_sqrt(2)
1.5
Looking to get:
my_sqrt(1.0)-->1.0
my_sqrt(2)-->1.41421356237
return ends the function and returns immediately. It's indented so it's in the loop and runs unconditionally at the end of the first iteration. Dedent it to run after the loop completes due to break:
def my_sqrt(a):
x = 1
while True:
y = (x + a / x) / 2.0
if y == x:
break
x = y
return y
Alternatively, since return ends the function and thus the loop, you don't even need break:
def my_sqrt(a):
x = 1
while True:
y = (x + a / x) / 2.0
if y == x:
return y
x = y
Related
I was doing this leetcode question:
https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/
My code was:
res = []
for x in range(1,1001):
for y in range(1000,0,-1):
test = customfunction.f(x,y)
if test == z:
res.append([x,y])
return res
but this timed out,
a solution was:
def findSolution(self, customfunction, z):
res = []
y = 1000
for x in xrange(1, 1001):
while y > 1 and customfunction.f(x, y) > z:
y -= 1
if customfunction.f(x, y) == z:
res.append([x, y])
return res
given the for loop and the while loop. it seems like these two functions do exactly the same thing.
Why does my code time out but the while loop doesn't?
The working version breaks out of the inner loop once the result of the custom function is reaches z.
You can get the same result using a break in the for loop.
for x in range(1,1001):
for y in range(1000,0,-1):
test = customfunction.f(x,y)
if test == z:
res.append([x,y])
elif test < z:
break
In the below code, the loops don't seem to be working.
powers = []
x = 0
y = 0
z = 1
for x in powers:
powers.append([])
for y in powers[x]:
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
x = x + 1
y = y + 1
z = 1
print(powers)
However, when I run this in the terminal, it simply returns an empty list for powers. It doesn't show an error message.
Please help.
your code is badly formatted, so please consider formatting, anyway some of your errors are code-comments.
powers = []
x = 0
y = 0
z = 1
for x in powers: #here you're overriding former declaration of x, also powers is empty so for doesn't have a single run
powers.append([]) ##code is not indented so this istruction execute only once
for y in powers[x]: ##powers[x] is an empty list
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
z = 1 ## this is outside the cycle
x = x + 1
y = y + 1
print(powers)
this is my code so far
x = 100000
while x < 100000:
y = x + 100000
z = (3*10000)-1/(10-3)
if y != z:
x += 1
else:
print(x)
break
I know the answer should be 42857 but it's giving me 10000
The loop never executes because of the wrong initialization of x. Besides, z is constant (doesn't depend on x)
In your code it probably prints 100000 because of the broken indentation that makes else statement match the while. Since no break occurs, the last value of x is printed.
Better do a for loop. That works:
for x in range (1,100000):
y = x + 100000
z = x*10 + 1
if y == z//3:
print(x)
break
else:
# for loop completed without break
print("not found")
or in one line, using next and a generator comprehension:
result = next(x for x in range (1,100000) if x + 100000 == (x*10 + 1)//3)
in both cases the result is indeed 42857
I am writing a program in Python that defines a function that takes a single argument. The function has to be a while loop the returns the largest power of 16 that it is equal to. However, I am not sure how to write the while loop.
Python Docs
while True:
n = input("Please enter 'hello':")
if n.strip() == 'hello':
break
so, in layman's terms
while <condition>:
...
I couldn't completely understand your question but here's how to do a while loop to get the x to the 16th power of an input:
def loop_function(x):
y = 1
start = x
while y != 16:
result = start * x
start = result
y += 1
return result
print loop_function(3)
The above code will return the answer to 3^16 which is 43046721
You can even make this a broader function two arguements
def loop_function(x, y):
z = 1
start = x
while z != z:
result = start * x
start = result
z += 1
return result
print loop_function(3, 2)
The above code will return 9 i.e 3^2
I am trying to write the simplest code possible that will continuously print out Perfect Squares. My code so far reads this
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
When I run it, I get a syntax error. The red bit is on the "final" in "print final"
Apologies if it is a really basic error, but I'm stumped.
Thanks for taking the time to read this
I assume you're using Python 3. print is now a function in Python 3.
Do print(final) instead of print final.
Also, it seems like your x and y values are holding the same thing. Why not discard one of them and use just one?
x = 1
while True:
final = x * x
print(final)
x = x + 1
Or better yet, use the builtin exponentiation operator **.
x = 1
while True:
final = x **2
print(final)
x += 1
Also, your code seems to be going into an infinite loop. You may need a condition to break it.
For example, if you want to break when x reaches 10, just add a condition in the while loop, like follows:
x = 1
while True:
final = x **2
print(final)
x += 1
if x == 10:
break
OR Specify a condition in the whilestatement, like follows:
x = 1
while x < 10:
final = x **2
print(final)
x += 1
OR Use a for loop.
for i in range(10):
print(i**2)
In Python 3, print is no longer a statement but a function, so you must enclose what you want to print in parentheses:
print(final)
Here's a link about the function.
You also have an IndentationError, y = y + 1 should be given a space.
And you can simplify that to y += 1 (which is the same thing, in regards to integers)
You can also add a condition to the while-loop:
x = 0
while x < 5:
print x ** 2
x += 1
Prints:
0
1
4
9
16
I would reccomend using Python 3 if you're not already. Also, as x and y are the same value at all times you only need one of them. So instead of reading:
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
You should write:
x = 1
while True:
final = x * x
print(final)
x = x + 1
Hope this helped!
Jake.