Jump to content

method: factorial

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
i traced the code and i got the how it works, the only thing i can't get is what is the purpose of int n? as i see, n is not being incremented.

for(i=1;i<=10;i++) {
            result = factorial(i);
            out.println(result);
        }

static long factorial (int n) {
            int i;
            long result=1;
            
            for(i=1;i<=n;i++) {
                result *= i;                
            }
                return result;
        }


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
n determines how many times it loops. It shouldn't be incremented. It's just a maximum value.

#3
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
how come it does not go infinite? is that a built-in code for java?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Assume n==2
Note that i++ is the same as i+=1

for(i=1;i<=n;i++)
i=1 -> for the first time
i<n -> condition checked every loop
i++ -> action done after every loop

First loop:
i==1
i<=n ? true
do result *= i
end of loop, i++,

Second loop:
i==2
i<=n ? true
do result *= i
end of loop, i++,

Third loop:
i==3
i<=n ? false
Leave the for-loop.

#5
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
for that case the n has a value. in the code i provided the n has no values. xd sorry, really confused.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
n must have a value or the program will crash upon reaching the for-loop. I'm guessing that that will result in a NullReferenceException.

In your code you can't reach it without giving n a value:
for(i=1;i<=10;i++) {
            result = factorial(i); --> [B]here you pass the value of i to factorial[/B]
            out.println(result);
        }

static long factorial (int n) { --> [B]here the i comes in, and is named n in this method[/B]
            int i;
            long result=1;
            
            for(i=1;i<=n;i++) {
                result *= i;                
            }
                return result;
        }
So during the run you will loop 10 times here
for(i=1;i<=10;i++) {
            result = factorial(i);
            out.println(result);
        }
Which will result in:
result = factorial(1); --> n will be 1
out.println(result);
result = factorial(2); --> n will be 2
out.println(result);
result = factorial(3); --> n will be 3
out.println(result);
result = factorial(4); --> n will be 4
out.println(result);
result = factorial(5); --> n will be 5
out.println(result);
...
...untill 10...

#7
hawil

hawil

    Newbie

  • Members
  • PipPip
  • 26 posts
java can do that? waw! now i understand.. thank you very much. the n really confused me..