Turn pseudocode into functioning code to find greatest common divisor [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the pseudocode here that says: Initialize d to the smaller of m and n.
While d does not evenly divide m or d does not evenly divide n do
Decrease the value of d by 1
Report d as the greatest common divisor of n and m
and I can't figure out what's preventing my code from working. I'll post my code below, if you could take a look for me and tell me what's preventing my code from working properly I'd appreciate it.
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while True:
if d % m != 0 or d % n != 0:
d -= 1
elif d % m == 0 and d % n == 0:
print(d)
break

To find if a value evenly divides another, you need to have the smaller (divisor) on the right side of the modulus operator. So change your code to:
if (m %d != 0) or (n % d != 0):
This evaluates if d evenly divides mor n, instead of if m or n evenly divide d.

The condition in your if is basically the opposite of your elif condition, and to evenly divide m means m should be the left side of the modulo operator (same thing for n). I made an edit here to show what I mean:
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while m % d != 0 or n % d != 0:
d -= 1
print(d)

Related

Getting Python program error: x is not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I made a program in python to calculate the roots of a quadratic equation by asking user for the coefficients.When I run the program for D>0, it is fine but for D<0 , it is printing imaginary solution but I am also getting the error : x is not defined. I want to remove that.I tried many other loops also but since I am a beginner at my own so I got struck , please help
import math
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
D = b * b - 4* a* c
if D < 0:
print("imaginary solution")
if D >= 0:
x = (-b + math.sqrt(D)) / (2 * a)
y =- b/a - x
print(x, y)
x is only defined when D >= 0, you have to either also define x when D < 0, or skip any code including x.
Import math
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = int(input("Enter c:"))
D = b * b - 4 * a * c
if D < 0:
print("imaginary solution")
else:
x = (-b + math.sqrt(D)) / (2 * a)
y = -b/a - x
print(x,y)
the problem is that x is assigned (and defined) only in a specific case,
one solution is to define it at the start of the program, as:
x=0

What am I Doing wrong. Google kickstart 2020 [closed]

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 1 year ago.
Improve this question
this the question.
Ron read a book about boring numbers. According to the book, a positive number is called boring if all of the digits at even positions in the number are even and all of the digits at odd positions are odd. The digits are enumerated from left to right starting from 1. For example, the number 1478 is boring as the odd positions include the digits {1, 7} which are odd and even positions include the digits {4, 8} which are even.
Given two numbers L and R, Ron wants to count how many numbers in the range [L, R] (L and R inclusive) are boring. Ron is unable to solve the problem, hence he needs your help.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of a single line with two numbers L and R.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the count of boring numbers.
Sample
3
5 15 Case #1: 6
120 125 Case #2: 3
779 783 Case #3: 2
And this is the result which I have written.
t = int(input())
for case in range(1, t+1):
total_number = 0
numbers = []
l, r = map(int, input().split())
for number in range(l, r+1):
answer = False
for x in range(len(str(number))):
position = x+1
pos_num = int(str(number)[x])
if ((position%2 == 0) and (pos_num%2 == 0)) or ((position%2 ==1) and (position%2 == 1)):
answer = True
else:
answer = False
if answer == True:
numbers.append(number)
total_number += 1
print(f"Case #{case}: {total_number}")
I am running this and getting the wrong results. Where am I doing wrong?
Sorry guys, I got the answer. This is the answer but if anyone can suggest a more efficient answer then, you are most welcome!
t = int(input())
for case in range(1, t + 1):
total_number = 0
numbers = []
l, r = map(int, input().split())
for number in range(l, r + 1):
answer = []
lst_number = list(str(number))
for x in range(len(lst_number)):
position = x + 1
pos_num = int(lst_number[x])
if ((position % 2 == 0) and (pos_num % 2 == 0)) or ((position % 2 == 1) and (pos_num % 2 == 1)):
answer.append(1)
else:
answer.append(0)
if 0 not in answer:
numbers.append(number)
total_number += 1
print(f"Case #{case}: {total_number}")

How do I check divisibility in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am writing a python script right now to check if a number a Wilson Prime, but it says I have a syntax error on line 9.
I've tried both the mod function with x%y == 0 and x/y == int, but both of them have given me a syntax error.
n = int(input("Type a natural number here"))
fact = 1
for i in range(1,n):
fact = fact * i
finalfact = fact + 1
if finalfact % n == 0
print(n, "is a prime number!")
if (finalfact/n) % n == 0
print(n, "is also a Wilson Prime!")
I'm trying to make it check if (n-1)!+1 is divisible by n (which is a way to find prime numbers) and to check if (n-1)!+1 is divisible by n^2 (Wilson Primes), but it gives me a syntax error on line 9 and nothing happens.
You are missing : at the end of if statements.
Change:
if finalfact % n == 0
To:
if finalfact % n == 0:
And:
if (finalfact/n) % n == 0
To:
if (finalfact/n) % n == 0:
Need to indent the code properly:
Change:
for i in range(1,n):
fact = fact * i
To:
for i in range(1,n):
fact = fact * i
Also:
finalfact / n # 5 / 2 = 2.5
Should be:
finalfact // n # 5 // 2 = 2

If N is positive then output all values from N down to and excluding 0. If N is negative, then output every value from N up to and excluding 0 [closed]

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 5 years ago.
Improve this question
As I said before I'm a beginner trying to learn Python in a college course, but what they teach us, doesn't always apply to the real world, and when I try to research a solution, there's nothing to be found. Anyway my latest problem is:
This is a tricky challenge for you.
We will pass in a value N. N can be positive or negative.
If N is positive then output all values from N down to and excluding 0.
If N is negative, then output every value from N up to and excluding 0.
and the starting code is:
# Get N from the command line
import sys
N = int(sys.argv[1])
# Your code goes here
counter = 0
while counter <= N:
print(counter)
counter = counter + 1
elif counter >= N:
print(N+counter)
counter = counter - 1
My solution produced an error:
SyntaxError: unexpected EOF while parsing
There is an error is your program.
And I honestly don't know where to start, as our textbook doesn't cover these challenges. Thanks in advance for any assistance rendered in this matter. BTW my code is inputted in Codio, and must pass in Codio's IDE.
# Get N from the command line
import sys
N = int(sys.argv[1])
# Your code goes here
if N > 0:
for x in range(N, 0, -1):
print x
if N < 0:
for x in range(N, 0):
print x
While Loops Version
# Your code goes here
if N > 0:
while N > 0:
print N
N -= 1
if N < 0:
while N < 0:
print N
N += 1
The main problem here is that you have a while and an elif. Elif is used with other if statements.
For example:
if condition:
do something
elif condtion:
do something else
else:
do something as a "last resort"
I think you are trying to do this:
#Checks To See if N is Positive
if N > 0:
#Sets counter to 1
counter = 1
#Iterates Thru Each Num from 1 To N-1
while counter < N:
print counter
#Increments Counter
counter += 1
#Checks To See If N Is Negative
elif N < 0:
#Sets Counter to -1
counter = -1
#Iterates Thru Each Num From -1 To N-1
while counter > N:
print counter
#Decrements Counter
counter -= 1
A couple final things:
If you want to learn what you consider "real world" programming, then I suggest that you look at some online resources such as https://www.codecademy.com/ or looking through the docs.
When posting on Stack Overflow, please explain your question/problem thoroughly
When inputting your code on Stack Overflow, make sure that it is properly formatted (e.g. indentations), as they make decoding larger programs much easier.
Stack Overflow is not a "do my homework for me" site

Python redundancy [closed]

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.

Categories

Resources