Question

Write the function get_factors(n) that returns a list containing all of the divisors (without remainder) of...

Write the function get_factors(n) that returns a list containing all of the divisors (without remainder) of the integer n. Use the built-in function range() to create the appropriate sequence over which to run the list comprehension.
Here are some usage examples:

>>> get_factors(10) 
[1, 2, 5, 10]
>>> get_factors(2017) # a prime number
[1, 2017]
>>> get_factors(60) # highly non-prime! why we have 60 minutes per hour
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
>>> get_factors(3147) # relatively prime – few factors
[1, 3, 1049, 3147]

Hint! Use a list comprehension with an if clause: x is a factor of n if dividing n by x has no remainder.

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

In this program,

- We have defined a function get_factors(n) that returns a list containing all of the divisors (without remainder) of the integer n
- Then we are using the list comprehension with an if clause to fetch all those elements which are the divisors of n.
- Then storing it in a new list.
- After that, we are printing the new list.

Code:

#Defined a function named get_factors which accepts an integer
def get_factors(n):
  
#Using list comprehension iterating from 1 to n with an if clause, then
#checking whether n after dividing with i leaves no remainder
#All such elements are then stored in the list named newList
newList=[i for i in range(1,n+1) if n%i==0]
  
#You can even print the newList or else return the newList
print(newList)

Please check the compiled program and its output for your reference:
In [8]: Defined a function named get_factors which accepts an integer def get_factors(n): #Using list comprehension iterating
Output:
In [10]: get_factors (10) [1, 2, 5, 10] In [12]: get_factors (2017) [1, 2017] In [13]: get_factors (3147) [1, 3, 1049, 3147]

(Feel free to drop me a comment, If you need any help)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Add a comment
Know the answer?
Add Answer to:
Write the function get_factors(n) that returns a list containing all of the divisors (without remainder) of...
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
  • Haskell Define function-divisors that receives a number and returns a list of all divisors of that...

    Haskell Define function-divisors that receives a number and returns a list of all divisors of that number.For example, if the input is 20, then the output would be[1,2,4,5,10,20]. You may use list comprehension, list ranges, and function mod for this purpose.Hint:Given numbern, consider all numbers from 1 ton, and then keep only the ones that dividen.

  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • Write a function vowelCount(S) that takes a string, S, and returns a new dictionary containing entries...

    Write a function vowelCount(S) that takes a string, S, and returns a new dictionary containing entries of the form v:N where v is one of ’aeiou’ and N is the number of occurrances of v in S.Your dictionary should never contain any entries where N is 0; thus, for example, if the S is "wipeout", the dictionary returned would be {′e′:1,′i′:1,′o′:1,′u′:1 }and not {′a′:0,′e′:1,′i′:1,′o′:1,′u′:1}. NOTE: must use a comprehension

  • Write a python function that returns a list or array of tuples containing every permutation of...

    Write a python function that returns a list or array of tuples containing every permutation of numbers 1 to n. For example: permutate(1): [(1,)] permutate(2): [(1, 2), (2, 1)] permutate(3): [(1, 2, 3), (1, 3, 2), (3, 1, 2), (3, 2, 1), (2, 3, 1), (2, 1, 3)] #here is some starting code def permutate(n): result = [] return result

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • See the image below and write the code using C++, without using variables, only recursion. 3....

    See the image below and write the code using C++, without using variables, only recursion. 3. 5 a [DO NOT USE ANY VARIABLES Write a C++ function that takes two integer parameters (a and b) and prints a list of all the integers between a and b-1 (inclusive). one (2, 12) should print 2 34567 8 9 10 11. one (2, 13) should print 2 34567 8 9 10 11 12 b. [D NOT USE ANY VARIABLES Write a C++...

  • Write a function findEvens that takes an integer, n, as input and returns the list of...

    Write a function findEvens that takes an integer, n, as input and returns the list of even integers between 1 and n. Ex. Input: 10 Output: [2,4,6,8,10] Write a function sortList that takes in a list of strings and returns a list of those strings now sorted and lowercase. Ex. Input: [‘ABE’,’CAD’,’gaB’] Output: [‘abe’,’acd’,’abg’] Both done in Python

  • IN PYTHON Implement a function printSeqs() that accepts a list of name lst and prints the...

    IN PYTHON Implement a function printSeqs() that accepts a list of name lst and prints the following sequences. The sequences are printed by the for loops found in the function. The information below shows how you would call the function printSeqs() and what it would display: Write a for loop that iterates through a list of names and print a greeting for each name in the list. Request from the user a positive integer n and prints all the positive...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined...

    Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined above and returns the expanded (un-compressed) string. You may assume that the first value of count_tuples has a count that is greater than or equal to zero and all other counts are greater than zero. Comments to show how to figure this out would be greatly appreciated A data file in which particular characters often occur multiple times in a row can be compressed...

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
ADVERTISEMENT