6 kyu
Lucas numbers
502 of 1,954NaMe613
Loading description...
Algorithms
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.
Please add support for the Rust language
python new test framework is required. updated in this fork
Approved
Input "-10" should be a positive Number... how? Same with "-30", there are some errors in the tests?! I got the same Numbers but negative - as in the instructions but can't complete this challenge.
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )Please add range value for
n
in the description :0 < n < 10000
( Java )
Python 3 should be enabled.
Enabled
@DarkD1, C++ expects a 64-bit
long long
result, but the model solution uses 32-bitint
's for its calculations. This results in overflow errors for large values ofn
. I was using 64-bit variables for all calculations and failing to pass until I "downgraded" to 32-bit variables. The model solution should uselong long
for its calculations. Only the input parametern
should beint
.Thanks a lot. I can solve it easily for your tip!!
Fixed.
C++ Translation
Please review and approve.
Please see the issue above. It's been 3 years and nothing has been done about it.
Could as well have been 8 kyu as there isn't any requirement for dynamic programming. (Java)
Fine kata but Lucas numbers are kind of Fibonacci:-)
I am not able to submit python solution because i get below error. It does not work for one test which is lucasnumber(100).
Process was terminated. It took longer than 6000ms to complete.
Your solution is to slow.
Hint: Try not to use recursion.
So the C# method declaration is as such:
public static int lucasnum(int n) { }
This defines it as return type of int, meaning it could only ever return values from (-2,147,483,648 to +2,147,483,647). In some of these comments I'm seeeing test cases that are for like -51, but at -45 the calc would be (-969,323,029 - 1,568,397,607 = -2,537,720,636). This would fall outside the acceptable range of an integer. As a result the calc goes wonky and returns some incorrect value.
My code passes all the basic tests, but the random tests tell me it fails. I have all values of [n] calc'd for the allowable limit of the integer data type, so I'm not sure why it keeps telling me its failing.
I'm not sure what you mean by
as the type is (and as far as I know always has been) long.
However I did recheck the tests and there was a small problem in the random tests that could have been causing your solution to fail, it has now been fixed.
Thanks
Thanks for the response. I'm always one to own up to my own mistakes, and I think I may have not knowingly changed the signature to an int. The tests fixes are always appreciated too, got it working this morning.
Using Haskell I can get the tests running, but when submitting, I'm getting the following error:
Whoops, sorry. Seems like I accidentially forgot to change
Integer -> Integer
toInt -> Integer
in the initial solution. Should be fixed. Just change the type of your function toInt -> Integer
and you should be fine.Works now, thanks
My code passes tons of tests, including all those left below but when I submit my code I get the error: "Process was terminated. It took longer than 6000ms to complete" Can I not use recursion to solve this problem? Is it too computationally complex?
I am also getting a lot of "submission timed out" errors.
What language are you using?
The "Submission timed out" is a Codewars internal thingy (check the GitHub wiki on more information about that). However, a simple recursive will fail, since each non-terminating call will run the function twice, resulting in an exponential runtime (O(2^n)). Just draw the callgraph on a piece of paper and you will notice that it get's quite big for small values like
5
.I'm using javascript.
The test cases in the javascript translation can be above 1000 so recursion is to slow.
How would one begin doing this without using recursion?
Exactly. Too slow in JS and I cannot see a better way for this other than recursion. When I try (-50) I don't even see the process ends.
OK, recursion is bad for this. I solved it in iteration methos.
The extension to negative integers is not fully explained in the instructions. Unless the "research" on external resources are part of the kata, these rules should be clearly explained.
I've modified the description, can please check and if it's ok resolve the issue.
I would suggest using L(-n) = L(n)(-1)^n
Why would
L(n) = L(n + 2) - L(n + 1)
not work ?example:
another one:
and so on ....
You're right it works fine.
Thanks for the quick resolve and special thanks for this kata :-)
Hi, I have tried the tests but I got failed message: -50 to -1 Expected: -45537549124, instead got: 28143753123 Test Passed: Value == 28143753123 Test Passed: Value == -17393796001 Test Passed: Value == 10749957122
I have passed my own tests. Are these tests ok? Test.assertEquals(lucasnum(-51), -45537549124) Test.assertEquals(lucasnum(-50), 28143753123) Test.assertEquals(lucasnum(-49), -17393796001) Test.assertEquals(lucasnum(-48), 10749957122) Test.assertEquals(lucasnum(-47), -6643838879) Test.assertEquals(lucasnum(-46), 4106118243) Test.assertEquals(lucasnum(-45), -2537720636) Test.assertEquals(lucasnum(-44), 1568397607) Test.assertEquals(lucasnum(-43), -969323029) Test.assertEquals(lucasnum(-42), 599074578) Test.assertEquals(lucasnum(-41), -370248451) Test.assertEquals(lucasnum(-40), 228826127) Test.assertEquals(lucasnum(-39), -141422324) Test.assertEquals(lucasnum(-38), 87403803) Test.assertEquals(lucasnum(-37), -54018521) Test.assertEquals(lucasnum(-36), 33385282) Test.assertEquals(lucasnum(-35), -20633239) Test.assertEquals(lucasnum(-34), 12752043) Test.assertEquals(lucasnum(-33), -7881196) Test.assertEquals(lucasnum(-32), 4870847) Test.assertEquals(lucasnum(-31), -3010349) Test.assertEquals(lucasnum(-30), 1860498) Test.assertEquals(lucasnum(-29), -1149851) Test.assertEquals(lucasnum(-28), 710647) Test.assertEquals(lucasnum(-27), -439204) Test.assertEquals(lucasnum(-26), 271443) Test.assertEquals(lucasnum(-25), -167761) Test.assertEquals(lucasnum(-24), 103682) Test.assertEquals(lucasnum(-23), -64079) Test.assertEquals(lucasnum(-22), 39603) Test.assertEquals(lucasnum(-21), -24476) Test.assertEquals(lucasnum(-20), 15127) Test.assertEquals(lucasnum(-19), -9349) Test.assertEquals(lucasnum(-18), 5778) Test.assertEquals(lucasnum(-17), -3571) Test.assertEquals(lucasnum(-16), 2207) Test.assertEquals(lucasnum(-15), -1364) Test.assertEquals(lucasnum(-14), 843) Test.assertEquals(lucasnum(-13), -521) Test.assertEquals(lucasnum(-12), 322) Test.assertEquals(lucasnum(-11), -199) Test.assertEquals(lucasnum(-10), 123) Test.assertEquals(lucasnum(-9), -76) Test.assertEquals(lucasnum(-8), 47) Test.assertEquals(lucasnum(-7), -29) Test.assertEquals(lucasnum(-6), 18) Test.assertEquals(lucasnum(-5), -11) Test.assertEquals(lucasnum(-4), 7) Test.assertEquals(lucasnum(-3), -4) Test.assertEquals(lucasnum(-2), 3) Test.assertEquals(lucasnum(-1), -1)
It seems to me that your code has both problems in computing the right numbers and giving it the right sign (+/-); for the latter, see my reply below to Neoslide comment.
Thanks for your answer. The test that says failed is this one: Test.assertEquals(lucasnum(-51), -45537549124) ?
-50 to -1 Expected: -45537549124, instead got: 28143753123 Which other number can bring me the -45537549124 if is not the -51 ?
I try with my own test: Test.assertEquals(lucasnum(-51), -45537549124) And works...
Thank you for your feedback
Please use three backticks to mark code:
Otherwise it's kind of hard to read your comments. That being said, the JavaScript tests have been changed lately. Did you try again? Also, if you're completely stuck, you can add your current solution as a spoiler in a comment.
My apologies for not having replied yet,could you please clarify how many tests you pass before the error occurs.
Ok bkaes! I think I am no stuck, I want to know if the value that gives as a solution -45537549124 is -51, like this test:
NaMe613, I pass 57 tests before the first fail.
The test that fails says:
Thank you for your feedback.
I've changed the tests a bit try now.
Ok! Now I pass the -1 to -50 but I fail on the random.
Thank you I am going to see why!
Finally it works! Thank you for your feedback and for your kata.
I think my problem where with big integers!
Hi,
I try to solve it, but the test are not right ... Test.assertEquals(lucasnum(-10),123); // This test is not good. it should be lucasnum(-10) === -123 and not 123
no ?
No, it is right; for negative numbers, you have to check this way: odd number=>negative value; even number=>positive value.
Oh ok, I misunderstand !
Ty !!!
My pleasure :)
And I think it was not fully explained, I looked it up on wikipedia.
I may be missing something, but I fear the random test cases in JS are not easily dealt with (not in a 6kyu kata, at least) big numbers, due to a lack of precision.
I'm not quite sure what you mean, could you please elaborate a bit ?
I am not sure that one can solve random test cases with bigger numbers in JS, due to precision issues in that language: everything is a double float (more or less) value, so anything beyond 15 digits of length loses precision.
I'm sorry but I still don't quite grasp what the problem is, can you please clarify.
Is it that the random tests expect a level of precision that is not 6kyu to provide?
Or that due to a lack of precision in calculating the answers it's expecting a number that is not actualy a Lucas number ?
Or something else entirely and I'm totaly off target.
Yup. The random numbers (or the result of
lucasnum
) in JavaScript is too large for an integer with 32 bits, therefore, JavaScript automatically switches todouble
. You need to handle comparisons on floating point values with care. You can use something like the function below to handle those:Alternatively you could make sure that none of the numbers exceed the range of an
int32
, e.g. use smaller numbers in the random examples.@bkaes
Thanks, I've added your
assertFuzzyEquals
function to the test case and I am using it for the random tests.@GiacomoSorbi
I hope this sorts things out.
Thanks to both of you for your help : )
JS now certainly works, but I think a similar issue is on other languages too; consider I was using the Decimal class with 50 decimal digits in Python and got this:
9479398183114544640861314325734704178452902509291400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L should equal 9479398183114544640861314325734704178452902509146734338936275702483155470586109600468782416434303102723062665537736846418815435968207663011L
Even if there is some version of it, fine tuning the right amount of digital precision, it seems to me that this version is harder than the JS version. And I think the same could go for Ruby.
I just tried it in Python. After I threw all recursive variants away and used an iterative one, it worked fine. However, I'm not sure whether the integral type in Python over-/underflows in this case, but according to the Python IDLE it changes to
...L
automatically. There shouldn't be any "precision" issues in Python.This comment has been hidden.
@GiacomoSorbi
It's quite posible that the Python version is a bit harder then the JS one this is due to the fact that the tests in the Python version can go up to 10000 whereas the JS version has a max of about 1300.
This comment has been hidden.
I must admit I was taken off guard, as usually one can use the same strategy with different languages to solve the same kata (well, more or less and only when the language itself allows it, of course), but going in the right direction in the end makes them of comparable difficulty.
I must also admit I was expecting for test value in the range of 6-7+ digits, like another kata to calculate the nth Fibonacci, thus making the iterative/memoization approach very unlikely to work.
Knowing the range or at least attempting the right way, it is far from being hard.
So, yeah, I must agree one more time with senpai's wisdom on this one too.
Solving this in Ruby: Recursion: passed tests, timed out on submit Memoization: passed tests, timed out on submit Math: passed tests, passed all submits except very high values (where I am assuming floating point becomes inaccurate)
So basically, a challenging Kata (for me anyway :)) ... I'm tempted to look at the solutions but also want the satisfaction of solving this on my own. Any hints on the right direction? Is there an approach I am missing?
I did it with memoization on Ruby: maybe are you trying a wrong approach? Feel free to ask if you think I may offer some tip or suggestion :)
Recursion is too slow/heavy for bigger numbers, while I don't know any way around the math approach (I assume you mean using a Binet-like formula).
This comment has been hidden.
Fixed Thanks for pointing that out.
No problem, it's what the beta stage is for! :)