Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

About explaining code of 100! factorial digits sum

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13075
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper.

Also reposted source.
 
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. I got new task to make this code count 100! factorial digits multiplication but not sum. Any ideas?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the task is :

" Multiply 100! factorial digits "
 
Mike Simmons
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe line 13?
 
Campbell Ritchie
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote: . . . The variable "sum" is misnamed, . . .

That was the "stylistic" problem I mentioned earlier.
 
Mike Simmons
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic