Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

About explaining code of 100! factorial digits sum  RSS feed

 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, would be nice if anyone could explain me what specific lines mean. Thanks.

18,31,33,34,35,36,37





EDIT: The source - http://ubuntuforums.org/showthread.php?t=800388
Author - Achelis
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us exactly what you're wondering about? What do or do you not understand about those lines?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Line 18: Declares a private static method named factorial that takes an int as an argument and returns a BigDecimal.

Line 31: Declares a private static method named addDigits that takes a BigInteger as an argument and returns an int.

Line 33: Declares a variable named result that is an int and initializes it to 0.

Line 34: Declares a variable named strSum that is a String and initializes it to the result of the expression sum.toString(), which is a method call to the method toString on the object sum, which is the argument that was passed to the method in line 31.

Line 35: A for loop that has a variable i as the loop counter, counting from 0 to strSum.length().

Line 36: An opening brace. What's more to explain there?

Line 37: strSum.charAt(i) gets the character at position i from the string strSum. That will return an char. To convert that char to a string, "" is 'added' to it. The resulting String is then passed to the method parseInt of class Integer to convert it to an int. (It's an overly verbose way to convert a single digit to an int). That integer is then added to result. Note that result += something means the same as: result = result + something.

If you don't understand basic concepts such as methods, variables, for loops etc., then have a look at Oracle's excellent Java tutorials, especially: Learning the Java Language.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We also expect you to QuoteYourSources. It's only polite to give the original author credit.
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper.

Also reposted source.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have got your answer already, but I can see two problems, one stylistic and the other a minor enhancement, in that code you found.
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. I got new task to make this code count 100! factorial digits multiplication but not sum. Any ideas?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul chris wrote:Hmm. I got new task to make this code count 100! factorial digits multiplication but not sum. Any ideas?

Maybe it's just me, but I've got no idea what that means. Can you try rephrasing it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your factorial method is already using multiplication. The variable "sum" is misnamed, since it isn't the sum of anything; it's a product.
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the task is :

" Multiply 100! factorial digits "
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid that doesn't make much sense. Is it possible they just want you to calculate the factorial of 100? All the code for that is already there - you just need to print the result.

Or perhaps you're supposed to calculate the factorial of 100, then multiply all the digits of the result? That would be kind of silly though, considering there's at least one 0 digit in there, so the result of multiplying everything would be 0.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Or perhaps you're supposed to calculate the factorial of 100, then multiply all the digits of the result? That would be kind of silly though, considering there's at least one 0 digit in there.

24 of them right at the end, for a start.
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Or perhaps you're supposed to calculate the factorial of 100, then multiply all the digits of the result? That would be kind of silly though, considering there's at least one 0 digit in there, so the result of multiplying everything would be 0.


Yes, that's right, but maybe I need to get that "0" on the java and in that way to prove it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well most of the code is right there. You say you need to multiply the digits rather than add them. Can you find the part of the code that is currently adding digits? You just need a few very small changes.
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe line 13?
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . The variable "sum" is misnamed, . . .
That was the "stylistic" problem I mentioned earlier.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul krov wrote:maybe line 13?

OK. Line 13 calls the method addDigits. Let's imagine you want to change the addDigits method to a multiplyDigits method. Aside from changing the name, look inside the current addDigits method. Where in line 31-40 is addition actually being performed?
 
paul krov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think line 37.


But the teacher now said to make not with BigInteger command but with "strings" maybe you can help me to make new program or edit this code?
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul krov wrote: . . . the teacher now said to make not with BigInteger command but with "strings" . . .
I can't understand what your teacher meant by using Strings for arithmetic. Please ask them for more explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!