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 4569194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:11:01+00:00 2026-05-21T19:11:01+00:00

how to write a power function which makes use of the following facts: To

  • 0

how to write a power function which makes use of the following facts: To raise x to a power n (where n is a positive whole number), if n is even, you can find the nth power of x by squaring half that power. For example, x^12 is x^6 * x^6. For odd powers, just subtract one from the power, and multiply the result for the smaller power by x. for example, x^13 is x * x^12, which is x * x^6 * x^6. Recursively, any power can be found with less work than multiplying x by itself the number of times indicated.
I came up with this

power x n
    | n == 0  =  1
    | x == 0  =  0
    | even n = ( power x (n / 2) ) * ( power x (n / 2) )
    | odd n = x * ( power x ((n - 1) / 2)) * ( power x ((n - 1) / 2) )

but I get an error saying ERROR – Unresolved overloading
* Type : (Integral a, Fractional a) => Integer
*
Expression : power 2 2

  • 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-21T19:11:02+00:00Added an answer on May 21, 2026 at 7:11 pm

    Just a remark now that your code is running: Haskell doesn’t automatically memoize functions, so you’re calculating the recursive calls to power twice in the last two lines. I would recommend to introduce a simple function sqr k = k * k and to use it. There are several ways: a separate function; a where clause; and let.

    I would prefer let:

    power _ 0 = 1
    power 0 _ = 0
    power x n  = let sqr k = k * k
                     half_n = n `div` 2  
                     sqrHalfPower = sqr ( power x half_n )
                 in if even n then sqrHalfPower else x * sqrHalfPower 
    

    As you can see pattern matching deals with the first two cases. Then let defines some useful expressions, which can be used later in in. Note that for odd numbers, (n-1) `div` 2 gives the same result as n `div` 2, so we can unify both cases.

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

Sidebar

Related Questions

I was always wondering how I can make a function which calculates the power
I simply want to write some code that makes use of recursion of functions
I'm trying to write a recursive power function without using math.h / pow(), but
I faced an interview. where I was asked the following question Write a function
I wrote a simple program which takes a predefined number to a predefined power
In arrow do notation, you can use the rec keyword to write recursive definitions.
I want to write a function that returns the nearest next power of 2
I write the following query: select id, (select NameEn from [Campaign] where id=CampaignId) as
i write some utility class but how to get name ? Can that send
Guys, I am trying to write a class in C# that can be used

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.