Question

5. Write a function called no_squares which takes an input parameter N, a positive integer, and returns: 1 if N is not divisible by a square and has an even number of prime factors -1 if N is not divisible by a square and has an odd number of prime factors 0 if N is divisible by a square For example, no-squares (10) returns 1 (since 10 = 2x5), no-squares (30) returns-1 (since 30 = 2 × 3 × 5) and no-squares (12) returns 0 (since 12 = 22 x 3).

How to solve it using Python?

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

Here is the code for you:

#!/usr/bin/python
def no_squares(N):
   factors = []
   i = 2
   #Lists the prime factors of N.
   while N > 1:
       if N % i == 0:
           factors.append(i)
           N /= i
       else:
           i += 1  
   #Sorts the factors.      
   factors.sort()
   #If a factor is duplicated, return 0.
   for i in range(1, len(factors)):
       if factors[i] == factors[i-1]:
           return 0
   #If the prime factors are even, return 1, and -1 otherwise.      
   if len(factors) % 2 == 0:
       return 1
   return -1          
print no_squares(10)
print no_squares(30)
print no_squares(12)   ?

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
How to solve it using Python? 5. Write a function called no_squares which takes an input...
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
  • DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a...

    DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a function called inch2cm that takes one number in inches as a parameter, converts it to the centimeters and prints the result. The program output is shown below. Input: 14 Output: 14 inches = 35.56 centimeter Write a program and create the following functions: shapes(): takes the shape name and a number as parameters, and calls the proper function to calculate the area. areaCircle(): take...

  • write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ,...

    write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ, that includes things with positive number attributes. The method returns an array of two ThingArrayQueues as output where the first queue includes all things with even values from input and the second queue includes all things with odd values from inputQ. Zero is considered an even number. The input queue should be empty after calling this method. *Queue has a mixture of things with...

  • For Python: Complete the function sum_v2 that takes an integer n as input parameter and computes...

    For Python: Complete the function sum_v2 that takes an integer n as input parameter and computes the sum of all odd integers between 5 and n (inclusively). Use FOR LOOP only and NO IF STATEMENTS.

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • 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...

  • Python Programming Write a recursive function that takes positive int n as its input and returns...

    Python Programming Write a recursive function that takes positive int n as its input and returns sum of the first n squares, i.e 12 + 22 +...+n?

  • Functions can return a string, not just an int or a float. Write a function called...

    Functions can return a string, not just an int or a float. Write a function called everOrOdd which takes an integer parameter and returns either "Even" or "Odd" based on the value passed. In main, prompt the user for a number, called num, then pass num to evenOrOdd and display the returned value on the screen. Keep doing this until the user enters a zero. Use the following run as an example: Enter a number: 11 Odd Enter a number:...

  • 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

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