• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with my algorithm...  RSS feed

 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I've been having some trouble with this. Basically, I need to read in 2 integers (for example, 10 and 15), then multiply them like so: a * a + 1 * ... b. So, for our example, we would need to multiply 10 x 11 x 12 x 13 x 14 x 15 and output the answer.

Here's what I have so far:



I'm not sure how to go about creating the proper algorithm to do the rest, though. I know I need to use either a While or For loop, but I have had no luck with getting the right algorithm down. Any help is appreciated, thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zack,
You can use either loop to do this. "for" is a bit easier as you get the loop index built in. There are four parts to a loop. Think about what each part should be and it will help form the algorithm here.
1) When should the loop start
2) When should the loop end
3) How should I increment/decrement the loop?
4) What should I do in the loop?

Feel free to a post a follow up question or what you think the four parts should be.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're not at all familiar with loops, here's the most common form:

All the questions Jeanne suggested are answered there. See if you can match up a tiny fragment of that code with each of her questions.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a variant of the recursive factorial problem.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't mention recursion when a simple loop can do the trick as well. PLEASE!
 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel stupid. I still can't figure out what exactly needs to be done to solve this problem.

I know I need to use a loop (a for loop) and I know how to use them, but I can't figure out the logical mathematics behind the problem.
 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...I made this WAY too hard for myself.



Thanks for the help, everyone.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, glad you fought through to the end!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!