Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:21:38+00:00 2026-05-26T04:21:38+00:00

From Python Problem Set The three functions work correctly but they require executing one

  • 0

From Python Problem Set The three functions work correctly but they require executing one at a time before moving to the next in order to get the final result. Is there a way to obtain the result from all three without having to query each one individually?

>>> import itertools

>>> def prime_factors(value):
    if value > 3:
        for this in itertools.chain(iter([2]), xrange(3,int(value ** 0.5)+1, 2)):
            if this*this > value:  break
            while not (value % this):
                if value == this: break
                value /=  this
                yield this
    yield value

>>> prime_factors(315)
generator object prime_factors at 0x01182468>

>>> def prime_factors_mult(n):
    res = list(prime_factors(n))
    return sorted([fact, res.count(fact)] for fact in set(res))

>>> prime_factors_mult(315)
[[3, 2], [5, 1], [7, 1]]

>>> def totient(n):
    from operator import mul
    if n == 1: return 1
    return reduce(mul, [(p-1) * p**(m-1) for p,m in prime_factors_mult(n)])

>>> totient(315)
144
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:21:39+00:00Added an answer on May 26, 2026 at 4:21 am

    You can combine the second 2, but the generator should stay a generator:

    In [1]: import itertools
    In [2]: from operator import mul
    
    In [3]: def prime_factors(value):
                if value > 3:
                    for this in itertools.chain(iter([2]), xrange(3,int(value ** 0.5) + 1, 2)):
                        if (this * this) > value:
                            break
                        while not (value % this):
                            if value == this:
                                break
                            value /= this
                            yield this
                yield value
    
    In [4]: def totient(n):
                if n != 1:
                    res = list(prime_factors(n))
                    prime_factors_mult = sorted([fact, res.count(fact)] for fact in set(res))
                    retValue = reduce(mul, [(p-1) * p**(m-1) for p,m in prime_factors_mult]), prime_factors_mult
                else:
                    retValue = n
                return retValue
    
    In [5]: x = totient(315)
    
    In [6]: print x
    (144, [[3, 2], [5, 1], [7, 1]])
    
    In [7]: print x[0]
    144
    
    In [8]: print x[1]
    [[3, 2], [5, 1], [7, 1]]
    

    You actually can combine all 3 and have the 1 function return a 3-tuple of what each ones return value would be:

    import itertools
    from operator import mul
    
    def totient(n):
        if n == 1:  return 1
        res = list()
        value = int("%d" % n)
        if value > 3:
            for this in itertools.chain(iter([2]), xrange(3,int(value ** 0.5)+1, 2)):
                if this*this > value:  break
                while not (value % this):
                    if value == this:  break
                    value /= this
                    res.append(this)
        res.append(value)
        prime_factors_mult = sorted([fact, res.count(fact)] for fact in set(res))
        return res, reduce(mul, [(p - 1) * p**(m - 1) for p,m in prime_factors_mult]), prime_factors_mult
    
    x = totient(315)
    
    # This would be the returned list from prime_factors(315)
    print x[0]
    [3, 3, 5, 7]
    
    # This would be the returned value from totient(315)
    print x[1]
    144
    
    # This would be the returned list from prime_factors_mult(315)
    print x[2]
    [[3, 2], [5, 1], [7, 1]]
    
    # The 3-tuple:
    print x
    ([3, 3, 5, 7], 144, [[3, 2], [5, 1], [7, 1]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem of upgrading python from 2.4 to 2.6: I have CentOS
Im trying to download share data from a stock exchange using python. The problem
I'm looking to create favicon.ico files programatically from Python, but PIL only has support
Simple problem, I have it solved .. but python has a million ways to
I have a problem with a Python 2.7 project. I'm trying to set a
I am calling a WSDL web service from Python using SOAPpy . The call
I am trying to SFTP from Python running on windows and installed Paramiko as
I would like to run a process from Python (2.4/2.5/2.6) using Popen , and
I've run into some behavior from Python 2.6.1 that I didn't expect. Here is
I'm porting some code from Python 2 to 3. This is valid code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.