• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Another way to write Math Power

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys
Any other way of writing Math power
Writing a program but I can't use math power

here is the simple program

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you calculate power in general? say if i have to calculate 2^5 then i will multiply 2 fives times.

 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a solution for your problem:



All the best
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you had used various incorrect variables such as nombre ,numbre, puissance so I removed them.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
salvin, we dont provide solutions... We let OP to find the solution..
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:salvin, we dont provide solutions... We let OP to find the solution..


Correct

Read my code carefully
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why are you using the keyword float?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And why are you using the keyword float?

Agree, the exponent will be int (Or perhaps long?) since both number and exponent are int.

 
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct, calculatePower(2, -1) is clearly an int
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vin Ash, pay attention to what you should be getting when is to the power of 0.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do it by iteration or you can do it by recursion. There are two well‑known recursive solutions one running in linear time the other in logarithmic time.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys
sorry if some variables are incorrect. The program is in French. I just translate it here so that you guys understand. sometimes I forget a few.
Still fairly new to Java so will try to implement what you guys are saying
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by doing core task, not the other way round. The rest you know well as we see.

So, you need to create a method which takes the base number and exponent as the arguments and multiplies base number with itself as many times as exponent big is.
For example: 3^4 is 3 * 3 * 3 * 3.
Maybe one of loops would be useful? Maybe recursive way as CR suggested earlier? Which way you're feeling more comfortable?

In recursive way by using ternary operators I think you could do that with one line. But it is not that clear probably as iterative way could be. Try both and see which you like more.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vin Ash wrote:Writing a program but I can't use math power...


Then remove it until you're ready to test, or only include for comparison purposes. And my first suggestion would be to add a pow() (or puiss()) method of your own, because that's what you're trying to write.

Second suggestion: Remove all that input logic for the moment and concentrate on the problem. I understand that your final program may need to deal with user input but, for the moment, stick to solving the subject of your post - "How do I re-write Math.pow() myself?"

Third suggestion: Deal with integral powers only for now - ie, don't try to re-write Math.pow(double, double).
I would have trouble writing a method that can return the result of 1.653 ^ 3.771 without resorting to using Math - and I've been at this lark a long time.

So, with all that in mind, I think my initial class might look something like this:and now all I (or rather you) need to do is write the pow() method.

Do you see the difference? I've isolated the problem, and now I only have ONE thing to worry about - getting my pow() method right.

Once I'm happy it's working, then I can add all the user input stuff; but until then, it's just a distraction.

HIH

Winston
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys but I am afraid I don't really understand a lot yet about this
Copy pasting also is not my thing
I 'd like to understand first
I like Liutauras train of thought
but how to I write that in java
I tried the ^ but it does not work
there is got to be a simpler way to substitute math power
The teacher told us it was simple and you can figure this out on your own
but so far what you guys are writing , it doesn't
The logic is but the commands to execute isn't
for me calculate power is not working. Gives error
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, please show the method you have tried to write. Only the one which suppose to calculate the power. Good if you understood the logic, syntax is the easier part.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There's a couple of things wrong with this line.

First, the ^ operator is a bitwise OR, not what I think you want. Second, the number produced by number ^ exponent is an integer, and you printf format is looking for a float.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vin Ash wrote:
The teacher told us it was simple and you can figure this out on your own
but so far what you guys are writing , it doesn't



I am going to speculate that your teacher already taught loops -- as the expected solution is likely repeated multiplication in a loop.

Henry
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Knute
Thanks Henry...got a feeling before you said it, I should be thinking loopwise
looks like i'll spend another couple of hours thinking how to loop this
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite right. You're doing too much in one go. As you been suggested already, please do not include any code which is not relevant at the moment to your main task - power the number.

Once again, you have to calculate 2 to the power of 3. How would you do that by hand? 2 * 2 * 2.
How about 4 to the power of 6? 4 * 4 * 4 * 4 * 4 * 4. So, 4 is your base and 6 is exponent. You could see it in some mathematics books writen as 4 ^ 6, it is the same (but not in Java).

However, lets go further, 4 * 4 * 4 * 4 * 4 * 4, how you get there? You multiply 4 * 4 and the result you get multiply by the 4 again, then the result with the next 4, ie:
(4 * 4) * 4 * 4 * 4 * 4
(16 * 4) * 4 * 4 * 4
(64 * 4) * 4 * 4
(256 * 4) * 4
(1024 * 24)

Think of it, that first operation 4 * 4 gives you a sum, then you need to multiply the sum by 4 again..
Loke at the short code below:
What it would print?
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Luitauras ....Henry got me thinking deeper on the loop as well
and I hop to it and came up with something similar to yours.
Here is my finished code and it works

But before..THANKS EVERYONE FOR THE BIG BIG HELP

Thanks Luitauras for trying to make it a bit easier as well




 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use the recursive solutions myself:-That version runs in linear time. 2²° takes approx twice as long to calculate as 2¹°.That version runs in logarithmic time; it take approx twice as long to calculate 2²⁵ as 2⁵. Reducing the two throw statements to one is left as an exercise to the reader. I recommend those methods be wrapped by preceding them with this code:-And followed by }

That way you have a utility class which you can enhance next time you have a function to write and you can keep it for ever.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vin Ash wrote:Here is my finished code and it works

Hang on. Shouldn't you create a method to calculate power? You crammed everything to one method "main". Remember, method name suppose to reveal its intention why is he for. I'd think create method with the technique shown earlier and name it puisPower or something. Not sure what this puis mean, btu anyway, give something meaningful.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
puiss is short for puissance which is Exponent in English
That is the aim of the program :to calculate powers
On this simple program I think things work but I guess I am not done yet
This small program is a precursor for a bigger one I am working on too.
Again I cannot use math power.
When I tried to incorporate what I got there , I saw the problems you are talking about
so I guess I have to rethink this through
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written recursive solution which takes (log n) time. I have not considered maximum limit or negative exponent scenario..


 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice!
 
What? What, what, what? What what tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic