Algorithm help!

Hi,

I have to do produce an algorithm that takes any positive integer greater than 0 and less than 500 and once i have done this I need to produce a:

A list of all the factors of the given integer .

A statement of how many factors the given integer has.

A statement of the sum of all the factors of the given integer.

A statement saying whether the integer is a prime number or not.

So for example:

User Input: 36

Output: The factors of 36 are: 1, 2, 3, 4, 6, 9, 12, 18, 36

36 has 9 factors

The sum of all the factors of 36 is 91

36 is not a prime number

Could anyone provide me with some help on how I would solve this problem and produce an algorithm for it?

Help would be much appreciated!

Thanks guys!

Charlotte

xxx

[829 byte] By [mishimaa] at [2007-10-2 23:23:18]
# 1

> Could anyone provide me with some help on how I would

> solve this problem and produce an algorithm for it?

>

Many here can help but please don't expect people to do the work for you. You have four basic problems to solve so start with the first and work out what you have to do to solve it.

sabre150a at 2007-7-14 16:01:43 > top of Java-index,Other Topics,Algorithms...
# 2

Take a close look at the function %

This is the "mod" function and it return the remainder of an integer divide

so for example since 7 divided by 3 has a remainder of 1 you would get

7%3 == 1

(was 3 a factor of 7?) similarly

8%3 == 2

(was 3 a factor of 8?) and

9%3 == 0

(was 3 a factor of 9?)

Now - if A is a factor of B what does that mean in terms of remainders? How can you use the mod function to find factors?

Enjoy!

marlin314a at 2007-7-14 16:01:43 > top of Java-index,Other Topics,Algorithms...
# 3
thanks marlin314. are you referring to java there though? I have to produce an algorithm and write it in pseudo code!Charlotte xx
mishimaa at 2007-7-14 16:01:43 > top of Java-index,Other Topics,Algorithms...
# 4

well, yes, % is a java operator and probably C and several other languages as well. I kind of figured that since this is a java forum it would be ok if I slanted my answer a little toward using java.

No professor will complain if you mix a little actual code in with your psuedo code. Since psuedo code does not actually run on anything you don't have to be quite so careful and it can look more like english. Thus you can write

for every number, i, from 1 to n

instead of

for(int i = 1; i<n+1; i++){

The purpose of psuedo code is so that you can outline how you are going to do something before you actually do it. It is like the difference between a recipie for how to make cookies and actually making cookies.

The recipie say put in a cup of flour. The reality is knowing which drawer the cup measure is in, where you keep the flour bin, opening the flour bin, dipping the cup measure into the bin ...

In real code you must include every last detail and there is no point in doing any of that detail work if you don't know what you are going to do.

To solve your problem you are going to have to write a loop. Do you know how big the loop will be? do you know where to start it? Do you know where to end it? Do you know what you are going to do each and every single time through that loop? Are you going to be printing things inside the loop? Before the loop? After the loop?

That is what your teacher wants to see, that you know what you intend to do.>

marlin314a at 2007-7-14 16:01:43 > top of Java-index,Other Topics,Algorithms...