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

  • SEARCH
  • Home
  • 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 7672143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:13:06+00:00 2026-05-31T16:13:06+00:00

I have been learning functional programming and I come to idea, to assemble mathematical

  • 0

I have been learning functional programming and I come to idea, to assemble mathematical operators.
counting -> addition -> multiplication -> power -> ... Naturally came out simple and most naive code to express this and it works! Problem is that I really have no idea why it works so well and whit such large outputs.

question is:
What is complexity of this function?

Code is in python:

def operator(d):
        if d<=1:
                return lambda x,y:x+y
        else:
                return lambda x,y:reduce(operator(d-1),(x for i in xrange(y)))


#test 
f1 = operator(1)       #f1 is adition
print("f1",f1(50,52))  #50+52

f2 = operator(2)      #f2 is multiplication
print("f2",f2(2,20))  #2*20

f3 = operator(3)      #f3 is power, just look how long output can be
print("f3",f3(4,100)) #4**100 

f4 = operator(4)      #f4 is superpower, this one does not work that well
print("f4",f4(2,6))   #((((2**2)**2)**2)**2)**2

f5 = operator(5)      #f5 do not ask about this one, 
print("f5",f5(2,4))   #

output (instantly):

('f1', 102)
('f2', 40)
('f3', 1606938044258990275541962092341162602522202993782792835301376L)
('f4', 4294967296L)
('f5', 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656L)
  • 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-31T16:13:07+00:00Added an answer on May 31, 2026 at 4:13 pm

    The disassembly tells you that no magic optimizations have been applied here, it’s really just a reduce over a genexpr. Python just seems to be up to this task, even if it surprises you.

    >>> import dis
    >>> dis.dis(f3)
      5           0 LOAD_GLOBAL              0 (reduce)
                  3 LOAD_GLOBAL              1 (operator)
                  6 LOAD_DEREF               1 (d)
                  9 LOAD_CONST               1 (1)
                 12 BINARY_SUBTRACT     
                 13 CALL_FUNCTION            1
                 16 LOAD_CLOSURE             0 (x)
                 19 BUILD_TUPLE              1
                 22 LOAD_CONST               2 (<code object <genexpr> at 0x7f32d325f830, file "<stdin>", line 5>)
                 25 MAKE_CLOSURE             0
                 28 LOAD_GLOBAL              2 (xrange)
                 31 LOAD_FAST                1 (y)
                 34 CALL_FUNCTION            1
                 37 GET_ITER            
                 38 CALL_FUNCTION            1
                 41 CALL_FUNCTION            2
                 44 RETURN_VALUE
    

    If you specifically look at your f5(2,4) call, it doesn’t perform so many operations, actually:

    >>> counter = 0
    >>> def adder(x, y):
    ...   global counter
    ...   counter += 1
    ...   return x + y
    ... 
    >>> def op(d):
    ...   if d <= 1: return adder
    ...   return lambda x,y:reduce(op(d-1),(x for i in xrange(y)))
    ...
    >>> op(5)(2,4)
    32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656L
    >>> counter
    65035
    >>> counter = 0
    >>> op(3)(4,100)
    >>> counter
    297
    

    65k additions, let alone the 297 for the exponentiation, are not even worth speaking of when it comes to hilariously optimized modern CPUs, so it’s no wonder that this finishes in the blink of an eye. Try increasing one of the arguments to see how this hits the border of quick evaluation very rapidly.

    By the way, operator is a built-in module and you shouldn’t name your own functions like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a physicist, and have been learning some programming, and have come across a
I have recently been learning F# and functional programming. One application I have found
I have been learning about the basics of C# but haven't come across a
I have been learning about various functional languages for some time now including Haskell,
I have been researching and playing with functional programming lately, solely to broaden my
I have been learning to use Map more (to become more functional programmer). It
I have been learning C++ for three months now and in that time created
I have been learning more and more javascript; it's a necessity at my job.
I have been learning to use Emacs for a little while now. So far
I have been learning C++ with some books from school that are from the

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.