Thanks for the cow Campbell. Appreciate it!
Campbell Ritchie wrote:Well done getting that far and for all the searches you have done. I think your effort so far merits recognition.
The choice of having the methods static is not mine. It's the way Hackerranks works internally. I can't change the class name (must be Solution) and since main is given as static, so the methods must be otherwise it won't compile in the site.
Campbell Ritchie wrote:Is that conceptually a pure function with no side-effects? If so, it is reasonable to make all methods static. Or is there some piece of information linked to several sizes of triangle, in which case you should maybe consider an object solution.
The main purpose there isn't the "beauty" of the code but the results. I agree with you though. By the way, will this type of changes affect the speed? (newbie question ).
Campbell Ritchie wrote:I think you should move all that code out of the main method, anyway. Consider making the methods smaller, but more of them.
It's a bit complicate yes. But it's due to the way we get the values in the sequence. It's different for odd and even numbers. But i'll think about ways of simplifying it.
Campbell Ritchie wrote:Have a look at the loop in lines 17-34. That looks complicated, but I think it can be simplified. You know you can speed up i % 2 by using (i & 1) == 0 (or != 0 or similar)?
Thanks for the info. Now that you mention it, makes sense that a bit manipulation would be faster than a remainder operation...
Campbell Ritchie wrote:You know you can speed up i % 2 by using (i & 1) == 0 (or != 0 or similar)?
If there is than didn't find it yet... Must think about it.
Campbell Ritchie wrote:Is there a faster way of finding τ0 than a linear search?
This one i didn't understand... What prime numbers? I use prime factorization in τ0 yes. But i did that cause i think it is faster than finding the divisors using the brute force remainder opperation from 2 to sqrt() of the number... Other than that i don't see any other primes... Of course it would be good and faster if i simply assigned divisors = 2 if the number is prime... But that would mean that i would need to test the number to see if it is prime... And that means calculating the divisors again...
Campbell Ritchie wrote:What is that about prime numbers in the integer list link you gave us?
I didn't even consider using a Map. I can use the indexes of the List or ArrayList or Array (whatever i use) to indicate the original number and the corresponding entry as the number of divisors...
Campbell Ritchie wrote:Why are you specifying a particular kind of List? I would have thought that a List was better than an array for most things, but I'll let you work out the details for yourself. Yes, there is no point in working out the same numbers twice, but please work out why a List would be a better store for numbers than something like a Map.
The eternal question of variable names...
Campbell Ritchie wrote:Note how choice of names for booleans makes the loop hard to read; it would be easier to read with needsFinding instead of notFound.
Well maybe... Since we need to square the number to calculate τ0 i guess that at some point it will go beyond the limits of long... Honestly i keep getting timeouts before that...
Campbell Ritchie wrote:Are you sure that all numbers will fit within the bounds of a long? What about τ0, which looks a bit like a factorial. Do you need a BigInteger object to handle those numbers?
The explanation is in the comments!
Norm Radder wrote:An explanation for the magic numbers: 48 and 55 would be useful.
Could the 48 be '0'?
Is the 55 'A' - 10?
char values are numeric and can be used directly without using their int values.
Campbell Ritchie wrote:Why do you think a List is better than an array? I am not convinced that follows.
Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (e.g., period, comma, colon, semicolon). In sound-oriented systems, the dot represents a short sound and the dash a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space or, simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short time during which no sound is transmitted. Is to be used the international version of the Morse code (only characters and digits).
Write an application that reads an English-language phrase and encodes it into Morse code. Also write an application that reads a phrase in Morse code and converts it into the English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.