Question

I try to define a PYTHON program that can print the largest factor of the number except for the number itself. Ex: enter n as 15, the program will print: 5. The factor of 15 is 15, 5, 3, 1, but 15 will not be counted in this case. If enter a prime number, it will print 1.

I am not sure what I did wrong here, please point out my mistake and advise how should I improve.

def largest_factor(n) the_number = n list1 = while n 0: start=n if the_number%start== 0: return start list1.append(start) # g

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

def largest_factor(n):
the_number = n
list1 = []
while n>0:
start=n
if the_number % start == 0:
list1.append(start) # get all the value into a list
n=n-1
list2 = list1[1:] # remove the number itself from the list
return max(list2) # print th e max value

EXPLANATION

You do not need to return in If statements and another If when the_number % start != 0 is not needed simply use slice and remove the first element from list which is the number itself which you want to find the largest factor.

Case 1: When you want to find largest factor of 7.

list1 = [7,1]

when use slice i.e list2 = lst1[1:] 7 will be removed and list2 = [1] and max of list2 is 1. So, 1 is the largest factor of 7.

Case 2: When you want to find largest factor of 15.

list1 = [15,5,3,1]

when use slice i.e list2 = lst1[1:] 15 will be removed and list2 = [5,3,1] and max of list2 is 5. So, 5 is the largest factor of 5.

CODE AND OUTPUT

def largest factor(n): the number = n listi =[] while n 0 start=n if the number start == 0: list1.append ( sta rt ) # get alldef largest_factor(n): the number = n listl = [] while n>0: start=n if the number % start == 0: listl.append ( start) # get adef largest factor(n): the number = n listl = [] while n>0 start=n if the number start == 0: list1.append ( sta rt ) # get aldef largest factor(n) the number = n list1 = [] while n>0: start-n if the number % start == 0: listl.append ( start) # get al

I have tried to answer the question as briefly and simply as possible if you find any problem in the solution provided ask in comment,

Add a comment
Know the answer?
Add Answer to:
I try to define a PYTHON program that can print the largest factor of the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone fix this python program? I'm trying to do three things: Create a function that...

    Can someone fix this python program? I'm trying to do three things: Create a function that takes in a string as a parameter. Then create a dictionary of characters to integers, where the integer represents how many times the number of times the character appears in the word. Create a function that takes two lists as parameters and returns the elements they have in common of the two lists. Ask the user to create a list of numbers and keep...

  • How to remove all occurrences of 2 from list2. Then display the list. Python import random...

    How to remove all occurrences of 2 from list2. Then display the list. Python import random list1 = [] for i in range (10): list1.append(random.randint(1,4)) print('List 1:') print(list1) list2 = [] list2.append(list1[5:]) print('List 2:') print(list2) list2.remove ******** (This is the part I am having trouble on!!) print('List 2 with all 2s removed:') print(list2) Sample output: List 1: [4, 1, 1, 3, 3, 3, 2, 4, 2, 4] List2: [3, 2, 4, 2, 4] List 2 with all 2s removed: [3,...

  • Something is preventing this python code from running properly. Can you please go through it and...

    Something is preventing this python code from running properly. Can you please go through it and improve it so it can work. The specifications are below the code. Thanks list1=[] list2=[] def add_player(): d={} name=input("Enter name of the player:") d["name"]=name position=input ("Enter a position:") if position in Pos: d["position"]=position at_bats=int(input("Enter AB:")) d["at_bats"] = at_bats hits= int(input("Enter H:")) d["hits"] = hits d["AVG"]= hits/at_bats list1.append(d) def display(): if len(list1)==0: print("{:15} {:8} {:8} {:8} {:8}".format("Player", "Pos", "AB", "H", "AVG")) print("ORIGINAL TEAM") for x...

  • Python 3 Question: All I need is for someone to edit the program with comments that...

    Python 3 Question: All I need is for someone to edit the program with comments that explains what the program is doing (for the entire program) def main(): developerInfo() #i left this part out retail = Retail_Item() test = Cash_Register() test.data(retail.get_item_number(), retail.get_description(), retail.get_unit_in_inventory(), retail.get_price()) answer = 'n' while answer == 'n' or answer == 'N': test.Menu() print() print() Number = int(input("Enter the menu number of the item " "you would like to purchase: ")) if Number == 8: test.show_items() else:...

  • PYTHON please help, stuck on this output and memory image! THANK YOU! Q3 7 Points import...

    PYTHON please help, stuck on this output and memory image! THANK YOU! Q3 7 Points import copy list1 = [11, [22, 33], [44, 55], 66] list2 = list1 list3 = listi[:] list4 = copy.deepcopy(listi) list1[0] = 100 list1[1][1] = 300 list1 += [77] list2 = list2 + [88] print(listi) print(list2) print(list) print(list) Please write output of the above program below (in the text box) and upload a picture of memory image? Enter your answer here Memory Image: Please select file(s)...

  • Please can I have a UML Class diagram for the Python program below: def main(): print()...

    Please can I have a UML Class diagram for the Python program below: def main(): print() print("Welcome to Caesar Encryption and Viginere Decryption Cipher") print() print("Please select an option") option = "c" while option == "c": hello = input('Choose Caesar Cipher encrypt 1:\n' 'Choose Viginere Cipher Decrypt 2:\n') message = input('Enter a message: ') password = input('Enter a password: ') if hello == '1': viginere_cipher(message, password) elif hello == '2': viginere_cipher_decrypt(message, password) print() print("Choose an option") option = input('Enter c...

  • Java Given the array, which has been converted to a linked list, I need to invoke...

    Java Given the array, which has been converted to a linked list, I need to invoke a remove method to remove a number from the list. The output for the first array should come out like this Now list1 = 24->18->3-7->null Now list2 = 31->-9->5->21->4->0->8->null Now lsit3 = 5->0->1->null but i keep getting this output Now list1= 12->3->7->null Now list2= -7->null Now list3= 5->0->1->null int arr1[] = {24, 18, 12, 3, 7}; int arr2[] = {31, -9, 5, 21, 4,...

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • a) Write a program that uses a while loop to print all divisors of a number...

    a) Write a program that uses a while loop to print all divisors of a number supplied by the user. The program should also print the sum of all the divisors and whether the number the user entered is a prime number or not. Note: The definition of a divisor is a number that divides another evenly (i.e., without a remainder) and the definition of a prime number is a number whose only divisors are 1 and itself. b) Implement...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT