3 kyu
N-Parasitic Numbers Ending in N
195 of 972LesRamer
Loading description...
Algorithms
Mathematics
Puzzles
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This is too easy to be 3 kyu, knock it down to 4 or make just multiplying with carry not viable.
thanks for your suggestion
I worked out a mathmatical expression. It only fails in some octal cases (8,6) (8,7).
I have no idea why it isn't working and I already asked a question a week ago.
Could you show the expected results? Because if I had the results, maybe I could see my mistake as no one aswered my first question.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I hope we soon get a node.js version that handles BigInt, I just modified my old code to use BigInt and now it won't work.
D translation
Thanks :)
COBOL translation.
.
This comment has been hidden.
Looks like I've managed to fix the tests in Ruby ...
This comment has been hidden.
This comment has been hidden.
Thank you for pointing this out. I wrote this as my first kata. Rereading the code, I understand what my original intent was with the "batch test" and why it is flawed.
I fixed the test -- I suspect '0' was the only invalid answer that would have passed the original tests. It might be interesting to see if we could find other answers than '0' that would fail it. Regardless, the fixed test validates much more now. It could have been much simpler had I done what every other codewars kata does -- test against an official/accepted algorithm. Despite trying to be different, it does nearly the same thing anyway. So, live and learn, I guess.
in javascript you can't use BigInt because the testing use old js code please fix the testing code sir
I've fixed the tests -- and I did it without BigInt.
If you're able to use BigInt to solve the Kata -- fine.
Otherwise, I can update the description to indicate that BigInt is not allowed since part of the challenge with this Kata is not having BigInt support as is the case for some of the other languages.
what you mean unfair to other languages ? java 11 and python 3.8 both have it
You mentioned BigInt along with 'please fix the testing code'. The javascript tests were previously broken. The broken tests have now been fixed. Adding BigInt support is a suggestion whereas a buggy-test is an issue.
Lack of BigInt is in no way more limiting for javascript than it will be for any of the other CodeWars supported languages that lack BigInt. Working with limited precision integers to compute the sometimes huge (occasionally 158+ bits) answers was one of my motivations for writing this kata.
I'm not technically opposed to BigInt, but I don't want to spend any more effort than a mouse-click to support it.
you only need to enable node 10/1 2
To be a 3 kyu kata, it really should be opened up to any base 3 to 36...
This comment has been hidden.
This comment has been hidden.
It was a fun challenge, what with having to convert a prepared Python3 code into Python2 and fighting to optimize it enough to squeeze through the tests in the time alloted. As usual, seeing the 5-line solution afterwards is hilarious, but it was satisfying to get my way :)
This comment has been hidden.
I believe this is fixed now.
I think you should update test cases (at least those for C#) for base 10, because for n = 5 correct answer is 142857 not 102040816326530612244897959183673469387755, because 5 is special case and period of 5 / 49 isn't the smallest parasite number possible. For n = 5 correct answer is period of 1 / 7.
142857 is 5-parasitic but 142857 ends in 7 NOT with 5 as the title "n-parasitic ending in n" and the "Special Parasitic Numbers" section clearly calls out.
I added the 142857 in a new "clarifying anti-example" section in the description in hopes of improving the understanding of this kata's requirements. I resisted creating this out of concern that I was creating a "wall of text" that either won't get read or that otherwise discourages kata attempts.
My favorite kata on here are terse, deceptively simple and their solutions profoundly satisfying.
This comment has been hidden.
google is your friend... https://en.wikipedia.org/wiki/Hexadecimal
(and no need to scream like that... x) )
I see, so I assume that I'm supposed to use A-F. The Hex function in python doesn't do that - is there something built in?
well you can always rely on printf
%x
(or%X
for uppercase) :"%x" % my_digit
Otherwise
(list(range(0,10)) + [char(c) for c in range(ord('A'), ord('Z')])[my_digit]
will work up to base 36, but I think you were looking for the first one.I don't understand what you mean by "The Hex function in python doesn't do that". hex(10) '0xa' hex(12) '0xc' I get the same on Python 2.7 and 3.6. Or did I miss something?
Getting an error with the test library.
Fixture.cs(7,20): error CS0246: The type or namespace name 'Result' could not be found (are you missing a using directive or an assembly reference?) Fixture.cs(8,20): error CS0246: The type or namespace name 'Result' could not be found (are you missing a using directive or an assembly reference?)
Fixed. You might need to reset it to see the change. Or just change the
Result
toExpectedResult
in the sample tests.Hello. I'm agree with user Kastalien There are an errors in this kata. For case with lastDigit = 2 and base = 8 exist more smaller integer 52 ( in octal: 2 * 25 = 52 ) than in kata solution (1042) And for case lastDigit = 5 and base = 10 solution is 5 * 142857 = 714285, wich is much smaller then 102040816326530612244897959183673469387755 I didn't encounter issues with base 16. May be due to structure of random tests.
first things first: read the description correctly:
while what you're suggesting is:
Thanks a lot, Blind4Basics! My fault.
Hello,
I'm getting error with testcase digit: 3, numberBase 16 on C#.
My answer is 1057262010144124151298821193. But it is not expected result in this test.
Could you share what expected result should be for this case, so I can debug my code?
Since there is only a fixed set of answers, I won't give it to you. Instead, I will show what you get when you mutliply your answer by 3...
You seem to be off to a good start reading from left to right. The product starts with 3, then 105726 which match the digits in your answer... but then the next digit in the product is 0 and the next digit in your answer is 2 -- so it fails from that point on. You should also note that your answer contains no letters -- most hex solutions are likely to contain some letters.
I don't know what your algorithm is, but from the lack of letter-digits [a-f] in your solution, I would guess that you wrote an algorithm that solved for base-10 and have a hardcoded 10 where you should have the number-base argument.
Good luck and I hope you're getting some satisfaction from attempting this kata.
Thank you for response, it really helped:) I forgot to convert remainders that are greater than 9 to letter-digits
I'm getting what I think is a bug in the Javascript version where string.charAt() is not working in the test cases (or possibly a single test case I can't tell).
I've even tried guarding and only calling string.charAt(variable) if (typeof(variable) === 'string')
I'm not sure what's up or if i'm missing something obvious.
UPDATE:
I changed all of my string.charAt(x) to string.substring(x, x +1) and i'm still getting the same error. then I tried:
function calculateSpecial(lastDigit, base) { return 1 }
I got the same bug so the Javascript test cases need to be fixed.
Not an issue, a question.
If you don't even provide the most simple things like error message and your code, how do you expect people to help you?
I don't see how this isn't an issue. It seems like the test cases are broken.
CODE: function calculateSpecial(lastDigit, base) { return 1 }
ERROR MESSAGE: TypeError: s.charAt is not a function at test at run at Test.describe at /runner/frameworks/javascript/cw-2.js:159:11 at Promise._execute at Promise._resolveFromExecutor at new Promise at Object.describe at /home/codewarrior/index.js:53:6 at /home/codewarrior/index.js:63:5 at Object.handleError
"calculateSpecial" is supposed to return a string (see the second sentence of the "Challenge" section in the kata description.) If you return a number instead of a string, you will get this kind of error. There are base-16 answers require letters in the resulting answers, so all expect strings.
I could code the javascript test implementation to coerce the type to string, but base-10 is the only case where that makes sense.
[Edit] It has been a while since I coded it, but as I recall, the javascript test validates that the answer satisfies the criteria rather than that you get the same result as an official algorithm implementation (this is in constrast to the many kata that are written that way.) Barring that the official algorithm could be buggy, this has the benefit that Code-Warriors could completely view the entire javascript test code without seeing the working solution.
[Edit] Because of this, I'm considering adding a type-test for each of my javascript kata.
No era tan complicado después de todo. Hay que tener cuidado en representar bien las cifras de acuerdo a la base.
nice kata, but feels more like 5kyu to me.
This comment has been hidden.
Miss random test cases (why not open the bases range? 2=>16 should be fair enough)
Octal, decimal and hexadecimal are conventional programming number bases, so I've included those in this kata. Binary is also conventional, but consider binary in the context of this kata: "find a binary number ending in 0 such that when multiplied by 0 has the same digits but starts in 0 (or conversely, a number ending 1 when multiplied by 1 has the same digits but starts with 1)" -- stated like this, binary really doesn't make sense, so I haven't included it.
The tests cover all the cases for the digits [2,base) so "random" in such a case would really serve just to shuffle the test ordering. Since I've taken pains in writing the tests to avoid giving away the answers, I'm not inclined to randomize their order. Cheaters abound, but they will have to be a little more crafty here.
If you enjoyed tackling this kata, I encourage you to create your own variation. You could include random tests and other number bases as you see fit. :) One really fun one might be Base-64, there would be enough variety there to consider random tests.
Regardless, thanks so much for your interest and feedback!
apart from the base 2, it would be quite easy to implement random tests for the other bases up to 16, without big modifications of the test suite, tho. That would be a good addition to your kata!
Some
hairyreadings for mathematical aware people to get a grasp of this problem:http://math.blogoverflow.com/2014/06/11/when-do-n-and-2n-have-the-same-decimal-digits/
Are the tests supposed to end if you get two wrong? At the moment, my solution works for base 8 up to
n=5
, then fails on 7 and 8. But then it just quits and doesn't test anything else. Is this supposed to happen? If so, that's rather annoying because I would like to see what other tests I am able to pass, that might help me figure out what's wrong with my algorithm.That's almost it: the test cases were put in three distincts
it
wrappers (base 10, 8 and 16), and the tests won't start the nextit
if an error was found (that's originally designed to avoid getting minutes of testing and tons of bad reports when a basic function is wrong). You can reproduce the tests without the wrappers in the sample test cases and check your answer (then of course you won't get to know immediately if it's wrong but you can notice bad patterns and check by hand)Just a hint if it's of any help; a number can never start with leading zeroes.
(0)_(0) grrrrrr
fun kata. I would suggest maybe giving another example (with radix > 10) to demonstrate that expected answers should be represented in the given radix (i.e. some hex output will contain a,b,c,d,e,f etc).
Maybe tests has error, but true:
2 (base 8) => 25 5 (base 10) => 142857 7 (base 16) => 1024E6A17 9 (base 16) => 13B
I comment my code for emumerate k (by wiki)
I thought the kata and its examples were quite clear, but you're not the only one that has struggled with the kata's requirements. So I'll apologize in advance for what might seem like excruciating detail as I try to provide necessary clarification.
A multiplication expression can be defined as having 3 parts: a multiplicand, a multiplier and a product. As a math equation, we would write:
multiplicand * multiplier = product
For our purposes in this kata, the multiplier is clearly
N
. The kata wants you to find/compute a multiplicand such that:Looking to your 7 base 16 number -- it satisifies the requirement: 1024E6A17 * 7 = 71024E6A1 I believe the official tests accept this as the only answer for N=7 in hex.
The rest of the examples provided in your post, though, do not satsify the kata requirement -- the numbers for the multiplicand do not end in N.
The N = 2 base 8 answer IS NOT 25
In base 8, 2 * 25 = 52, which almost seems to satisify the kata; except, that this has reversed what is desired by the kata -- in this case, the product ends with N in the ones' place instead of in the one's place of the multiplicand like the kata seeks. (However, this might make for an interesting sub-kata/experiment. I suspect that there are likely few solutions that shift in this fashion.)
There are several approaches
One way is to consider how we would progress if we did it by hand. For N=4, Decimal, I did:
So, the last digit of the product must be 6, and we must have a 1 carry. Because of the same-order-digit requirement (in bullet point 2 above) we could proceed to find the next digit by using the information in this paragraph like so:
So from there, we get the last two digits of the product are 56 and we see that we have a 2 carry.
Proceeding similarly from there, eventually ends up with:
102564
which is the answer for the multiplicand that the kata is searching for in the Decimal N=4 case.How do we know when to stop? That's left up as something for the reader to discover.
This comment has been hidden.
(I've stepped away from codewars so, I apologize it took this long before I noticed your message.)
I felt I was clear both in the title and the description that what is sought is "special-parasitic numbers ending in N". I gave examples in the description and I note that the answers for all of the base-10 portion are under the "strict definition" section of the wikipedia link that I provided in the original kata description.
So, while 142857 * 5 = 714285, 142857 does not end in 5 like the title and description request. The smallest that ends in 5 that is parasitic is 102040816326530612244897959183673469387755.
I suspect what was confusing was that my meaning of "smallest," as defined in the description, differed from a similarly named section of the wikipedia link. In the 104 hex * 4 = 410, we could have repeated 104 any number of times and the property still holds. 104104104104104 hex * 4 = 410410410410410 hex -- the number the kata wants is 104, not 104104, 104104104, etc.
IMHO, the fun challenge here is that the "ending in N" portion is what pushes some of the solutions into large numbers which cause problems for brute force approaches.
(Note that the solution for all base-10 answers also appear in that strict-definition section.)
I don't have a problem with the wording, but I'm open to suggestions that would improve the kata.
Well, I'm not sure if by "brute force" you meant the wikipedia method, but it worked pretty well :p
Nice kata
Brute force doesn't generally seek to exploit relationships or break the problem into smaller pieces. By "brute force" I mean something like the approach a colleague used in his attempt to solve for just the base-10 parasitic number ending in 6. His approach was to start with 6 and increment that by 6 and then test the digits. Of course, he never completed the puzzle and that approach was doomed.
I got a solution that make pass the 'run exemples' and also the exemple i found over wikipedia. However, it fail on the attempt (Some fail and one forever loop). I can't figure out why.
Is it possible to add more exemple (with other base & value) or a least for most of the tests that fail when submitting, give the expected?
There's only a fixed set of answers, so I chose not to show expected values. I'll give you a couple of hints:
Good luck! Thanks you for your interest.
EDIT : Forget this comment. In fact my algorithm seem to be completly wrong.
I hope you're feeling engaged not discouraged. This is one of those puzzles that I feel gives a sense of accomplishment after completing it.
Don't know if i will be happy or if i will be sad thinking about the time spent on this simple exercise... I now got a solution in full math that seem to work. My first attempt manipulated strings...
But ! Some tests always fail (22 tests pass/28). The 6 faillings test are the 6 in base 16 where n >= 10. For exemple : calc(10, 16) return me "1019c2d14ee410" which is "1019c2d14ee4" + "10"
EDIT : In fact, i implemented https://oeis.org/A092697 so it logicaly fail. Does i should implement https://oeis.org/A128857 instead?
Is it possible to know the result of calc(10, 16) ?
These are interesting links. I'll give a big algorithmic hint that I may later flag as having spoiler content.
Let's take the simplest base-10 problem: the 4-parasitic number ending in 4 is 102564 -- let's pretend we don't know the answer and see if we can figure out how to derive the 10256 part.
If you wrote the problem out starting at the right and worked left:
This means that a number ending in 4 times 4 gives a product that ends in 6, so 6 is the next digit to the left of 4. We also figured out that we have to deal with a 1 carry. Remember that the same set of digits are going to be in the top and the bottom, just shifted and the 4 rotated around to the beginning.
So in the number on the top, write a 6 with the 1 carry over the top to create the next iteration for the multiplication:
So now we know the two digits to the left of the 4 in the multiplicand is 56, with a 2 carry. So expand the next digit out (note the carries across the top)
I'm going to quit spelling the findings out and just write the rest of the iterations out:
The rest is really just a coding this up and accounting for which number-base you're dealing with.
Yes, this is the exemple written over wikipedia. I base my first attempt on this algorithm with this exemple. It's work well for calc(4,10) and calc(4,16)
But now, let's try this algorithm with other number in base 10 : 2. calc(2,10) should result 105263157894736842 (Verify thx oeis.org/A092697/list)
If you wrote the problem out starting at the right and worked left:
2 x2 == 4
This means that a number ending in 2 times 2 gives a product that ends in 4, so 4 is the next digit to the left of 2. We also figured out that we have to deal with a 0 carry.
So in the number on the top, write a 4 with the 0 carry over the top to create the next iteration for the multiplication:
0 42 x2 === 84
So now we know the digit to the left of the 4 in the multiplicand is 4, with a 8 carry.
80 42 x2 ==== 84 <-- almost same answer - the only difference is in the carries.
And If i continue i have an infinite loop.
So where's the fail? :x
The reason I chose the '4' problem is that it's the shortest in base-10. Your problem is that 8 isn't the carry, it's the next digit; 0 is the carry. In multiplying the next digit and adding the carry, the product will always be 1 or 2 digits; the digit in the tens-place is the next carry and the digit in the ones-place is the next digit.
I've worked the first several out below.
All you have left to determine is the stopping criteria and generalizing this algorithm for any number base. This should get you really close without taking the rest of the fun out.
Thanks for your help. :)
I did not arrive to make it work with your explanation but after a quick look to your solution, i made almost the same thing. My concern was probably the stopping condition.
But i finish the exercice. How?
After for modification to my algorithm, i was able to get to the response of calc(10,16) and calc(11,16) (from the attempt) and I notice that i was exactly the same that the results i get with my pure math function except the last number that was not in the right base. So i simply change into the right base the last number return by the fonction.
It solve the exercice but :
I am far from being good at math and I may have misunderstood some things in the links I have given but the results for n> 9 and n = 5 in base 10 may be wrong.
The exercise remains interesting. :)
In the test case, the method is calcSpecial, but when you submit, it needs to be calc_special
Thanks. Should be fixed now.
Nice, but you should precise the definition used here isn't the same one as the one in the link you give.
From your link (wikipedia):
It quite differs with your definition:
You are correct. Thanks for your input. I've updated the title and the description to indicate that the kata is looking for special n-parasitic numbers that end in n.
Finding smallest n-parasitic numbers in base b isn't so difficult too as you only need to search a numerator between n + 1 and b wih the same denominator.
This comment has been hidden.
This comment has been hidden.
Nice kata, with a number of different ways to solve. Thanks.
This was a fun kata for me to author and I hope it is as fun for you to solve. It involves some number awareness.
I solved the puzzle a completely different way in java than I did in c#. I also know of a third, completely different way to solve the puzzle.
I'm really looking forward to solutions from you clever folks out there. Happy puzzling!