This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Store String and array of digits

 
Saloon Keeper
Posts: 6741
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Presumably it would make no significant difference to the performance of your first method if you passed an int[] instead of the long[]. Probably no difference to the other two, either.


Certainly would not have made any difference in #1. For #2 an #3 I would have had to introduce a mapToLong() so that the reduce() could accumulate a long. This would result in each character in the string being converted 13 times. In the end, probably not significant. The skip/limit approach was definitely a problem and resulted in non-linear scaling.

Edit: This is just a curiosity for me at this point. The problem domain is so small as to make efforts to optimize it for performance insignificant. However, I view this as an opportunity to learn so that I have a better grasp for when the time comes to implement stream processing for a problem domain where it would make a difference.
 
Bartender
Posts: 3674
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hinted at some optimization a couple of replies ago, but the reactions were such that the set of people interested probably equals the empty set.
And as Carey remarked, it is a waste of time trying to be smart for such a small problem.
But optimizing is sometimes more interesting than the problem itself. So here goes.

Let S0 be the product of the first 13 digits of, what Dana called, bigNumber.
So, S0 = d0 * d1 * d2 * ... * d12.
The product of the next 13 digits, S1, is: d1 * d2 * ... * d12 * d13. or: S1 = S0 * d13 / d0.
And so is S2 = S1 * d14 / d1, et cetera.
This resembles an iteration.

1) suppose that bigNumber does not contain any '0''s. We could then say:

Now, this 'alpha' is of the form d(i + 13) / d(i), so how to get this dynamic factor into the longstrean?

To test that all this is correct, we could use:

2) now, unfortunately, bigNumber does contain a lot of '0', totally ruining the above scheme.
Can you think of a simple way to deal with this problem, at the same time optimizing the excersize even further?
I must confess that for this, I used plain old java 7, since java 8 really got complicated.

Well, happy extended  puzzling!
 
Carey Brown
Saloon Keeper
Posts: 6741
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my highly optimized but convoluted method in Java 7. I think it is along the lines you were mentioning. First, compute the product of 13 numbers. Then, slide the window one number and compute prod = prod / number[i] * number[i+13]. Where this gets tricky is that when a zero is encountered it will skip to the next number past the zero and refill the buffer. I haven't timed this yet but the number of divides and multiplies it performs is 1,132 whereas the normal processing has 12,844.


Edit: timing

My version 3 takes about half the time of the simple version 1 approach.
 
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You need to parse the char, or convert it to a String.

If you're trying to get one digit at a time, and you know your input is a digit, then the easiest way to convert a single digit to an int is just

intArray[i] = Character.digit(s.charAt(i), 10); // in base 10

If you want to keep using Integer.parseInt, then just do

intArray[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
// or
intArray[i] = Integer.parseInt(s.substring(i, i+1));

 
Piet Souris
Bartender
Posts: 3674
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I simply use int d = string.charAt(i) - '0'.

Anyway, my solution was:

And for those going for their OCAJP, who also happen to be interested in this probem: if I replace line 53 with:

I get an incorrect result. Please explain.
 
Carey Brown
Saloon Keeper
Posts: 6741
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:And for those going for their OCAJP, who also happen to be interested in this probem: if I replace line 53 with:
I get an incorrect result. Please explain.


The formula you want is
what you have is
And since the divide will take place first and the divide is done in integer arithmetic it will truncate any fractional portion.
 
Piet Souris
Bartender
Posts: 3674
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet wrote wrote:And for those going for their OCAJP (...)


Don't tell me you're doing the OCAJP!  
But you're right, of course.
 
Sheriff
Posts: 7419
504
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed, have a cow, your topic has been chosen to publish in our April's journal
 
Ranch Hand
Posts: 458
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for cow but I don't have a good explanation.

 
The moustache of a titan! The ad of a flea:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!