Retired
Recursion #1 - Factorial (retired)
6,007 of 8,336gabrielsiedler
Loading description...
Fundamentals
Recursion
Mathematics
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.
Should return n and fact (n!) -- SQL
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/190.
Please join the discussion to help us identify duplicate kata and retire them.
https://docs.codewars.com/authoring/guidelines/translation/
"Do not translate kata into languages where the difficulty would significantly differ"
The SQL version is significantly harder
Should it really exists as a translation ?
Functions in SQL are an advanced issue.
The translation was created before the doc page you link to was written.
Anti cheat is hard, I know... (JS)
https://www.codewars.com/kata/reviews/5694d307531bb7194d00000f/groups/630dcfb192865d00016fe38b
It's a 7kyu and it tries quite hard. Do you really think this blatant cheat is a kata issue?
Note: not calling you a cheater, calling that solution a cheat. I don't see other people cheating ( that way ), so I don't really see the problem.
Very bad description. It is not clear what output data is expected, the final number, an array with a sequence of numbers, or a table (number, value). The tests are poorly designed, you can simply write the desired result with fixed data.
It says you have to return the factorial of n, or n! How is that not clear?
P.S.: I see you meant the SQL version, so you have a point there. Please mention the language you're talking about next time. There are some other posts about the same.
I apologize for not specifying the language. It was just a little frustrating that I had to guess what result is expected.
There is nothing to guess:
SQL version is indeed different. Read the posts below, like this one: https://www.codewars.com/kata/5694cb0ec554589633000036/discuss#5e483169dd38d00015623356
All the task instruction for SQL is "You have to create the function factorial that receives n and returns n!. You have to use recursion" but the test expects 17 results instead of a function. Even if the test accepts a function as result, including the keywords from the test is still way too difficult for 7 kyu
Ranks of approved katas cannot be changed.
Please see the first half of my comment, the issue is not resolved
I don't understand which issue you are talking about. What is incorrect in the tests?
The tests for SQL should expect a function "factorial", not a table of results. There is 0 description about how a table should be returned or how there should be 17 results in the table or what the 17 results should be. Here's the full description of the question:
Initial code for SQL:
But I agree, description should be improved.
Description for SQL would make Lacedaemonians look like motormouths.
I have passed all tests exept one, which says:
RangeError: Maximum call stack size exceeded
in javascript. And I can't sumbit it.I assume you have solved it by now? The solution I can see from you passes and also is in an accepted solution group by 79 other people.
This was extremely hard, but incredibly satisfying to solve. Cheers.
Fork which fixes markdown headers in description (author inactive)
This comment has been hidden.
On postgresql and I get error that it should recieve X Values?
For ones who has
PG::NumericValueOutOfRange: ERROR: integer out of range
check 4.1.2.7. Constants of Other Types or this Stack Overflow answer. Documentation about recursive request creation could be found here 7.8.1. SELECT in WITH.This kata has poor description but brings profit. Thanks @eliseuvideira for the comment. And thanks the kata author for his work too.
It was really hard to figure out what to do because:
This is kind of mess in description that makes people go wrong way many times during solving the kata. Please make instructions clear for SQL
This comment has been hidden.
The SQL version of this kata should contain better instructions, like that the result should contain 17 rows, with numbers from 0 to 16 as a column named "n" and another column named "fact", with the factorial value of "n", also that you are not allowed to use CREATE FUNCTION, intead should use WITH RECURSIVE cte.
Helpful comment, thank you.
Requirements: should return 17 items should have the correct column names should have the correct values should contain keywords should contain SELECT should contain RECURSIVE should not contain ! should not contain CREATE TABLE
Beeing honest... I had to hardcoded it to pass it.
Training in PostgreSQL 9.6
Expected 17, Got 0
Factorial of what number is 17? The question expects something it doesn't explain. Why should the function return 3 values?
Does not appear to be working for SQL (postgres 9.6). It says expected 17 results, 0 found (or something similar). I validated my function against a real psql 9.6 and it works.
This can be down with a CTE or a function, and recursion actually implies their use.
It's not clear whether a row with
n = 16
should be included or not.Fixed
SQL: https://www.codewars.com/kata/reviews/5d8e9366f9479b00017c4c82/groups/5db5fcf02010cb0001fb19b5
x)
I have no idea how to fix this, though...
me neither... Maybe an extensive regex check, but users will always find a way through it, I guess...
Limit code length? This solution is approximately 5 times longer than a valid approach.
some valid ones are as long as this one, tho... :/
Table creation can be banned because it's obviously the wrong approach. And
!
should be banned too.done
How's this 7 kyu in SQL, lol?
Too hard for 7. Pls bump up to 5
I think you're talking about SQL, rank is shared amongst all languages, can't be changed and in no way it is 5 kyu in those other languages.
I am trying to solve this kata in sql, but I don't understand how the solution is evaluated. I have created a function that does the what is requested, so if I do: SELECT factorial(3) I get the correct result of 6. Inputting the other values also returns the correct answer. Can anyone help?
(TypeScript): The requirement that "it calls itself" doesn't allow for tail call optimization using an inner "tail-call optimized" function. Was able to pass the checks with unnecessary conditionals for 1 and 0 in the "outer" function. :/
SQL Translation kumited! Please accept :smile:
The SQL top solution isn't recursive isn't it? Testing suite should be revised.
Please see issue above
Javascript does not work.
Not a kata issue, but a codewars issue, works fine with my code tho.
SEE BELOW~~
Don't working :(
Please put a lot more details than this when raising issues. Language, error message, expectations, etc, etc. A full explanation helps us to solve the problem.
CoffeeScript Translation kumited! Please accept :D
TypeScript translation
This comment has been hidden.
This comment has been hidden.
Anti-cheating methods probably shouldn't be this way. As someone else mentioned, it should be a random test.
Normally I would agree. But there is a limited number of testable outputs here due to factorial's tendency to overflow from exponential growth.
For example, you cannot go above 18! without exceeding javascript's and typescript's MAX_SAFE_INTEGER, nor can you go above 20! without overflowing csharp's ulong.
Nice kata!
C#-Translation kumited!
https://www.codewars.com/kumite/5795431e3467dbdb140001d4
The anti-cheating test is very different from yours. But your way is not possible for C#. But my anti-cheating test works, too. :-)
Kata is in a non-working state and even if you submit a previously accepted solution there is no way to pass the tests. Suggest it be pulled back to draft mode until the tests are fixed.
It works fine for me in JavaScript. Where was the problem?
His fix did work and I was able to submit issue resolved.
Perfect!
I keep getting below's error when I try to run the test. I even copied and paste someone else solution in this Kata, I still get the same error, can anyone fix the problem (or swtich back to old javascript).
SyntaxError: Unexpected token > at Object. (eval-wrapper:6:22) at at evalScript (node.js:536:25) at startup (node.js:80:7) at node.js:906:3
Yeah this Kata does not work and the Author has not taken the time to fix it. He should either fix it or unpublish back to draft mode.
Codewars wasn't allowing me to change the node version to allow arrow functions. And my code was running on an older version (donno why). It seems to be fixed and I could change it now.
This should be ok now. Let me know if it is solved.
Still is not working. Your tests are broken.
SyntaxError: Unexpected token > at Object. (eval-wrapper:6:22) at at evalScript (node.js:536:25) at startup (node.js:80:7) at node.js:906:3
Actually it is not my tests. It is the node version that the code is running. For some reason I still can't change it. It doesn't matter if I change it to
Node v6.0.0/Babel
, it stills sets toNode v0.10.33
, and this version doesn't allow arrow functions.The error that you are seeing there (
const require = _ => void 0;
) is an internal error of codewars regarding to arrow functions.I'll still try to change it, but don't know if I will be able to.
You are correct it is defaulting to Node and arrow functions do not work. I was able to change it this time.
This is preventing both the test cases and the submission tests from running, meaning this kata can not be completed. Since this has been an issue for a month, maybe converting the kata to regular old javascript might be an option?
Fixed.
Issue with test case. Throwing error even with no code or empty function.
It is actually a bug related to code wars. I can't set the node version to babel and it doesn't support arrow function as default. When it gets fixed I update it here.
Thank you for the feedback.
Oh that is pretty weird. Just going to mark as resolved.
I also can't seem to get past this error - I'm not sure it is resolved.
When I say resolved, I mean there seems to be no way for this issue to be fixed from the author's perspective as it is not a kata issue, but a codewars issue. For me, marking a kata as having an issue means the kata itself has that issue, not the platform on which it is hosted.
it said that my tests passed initially, and when I clicked it again it said that they didn't pass. when I clicked on the run tests for the third time, the tests then passed again.
Can you specify the error, please?
Couldn't reproduce and there is a lack of explanation.
Great Kata, this is the first recursion Kata that I have completed without any help :D
I can see your test cases are trying to check whether solutions are hardcoded but this is not the way to go about testing hardcoded solutions, Random test cases are always the best way to eliminate hardcoded solutions :)
basically I'm saying it needs random test cases.
I don't think this kata should be perfect in this tests you mentioned. If someone else opens a issue like that I will reconsider.
Exact duplicate of: http://www.codewars.com/kata/the-elegance-of-the-code/javascript Even the test cases are basically the same.
You are marking duplicate of a beta kata that has 22 issues and, as I can see, won't be launched, because it doesn't seem to be maintaned.
The test cases are very different. Only the obvious restrictions have to be the same: you don't want to test
factorial(7584)
because you are using recursion and you have a stack to work with, so you'll test for basic cases (from1
to a small number like15
)Also, I won't restrict the user's input because I could accidentally block correct results, so I'll catch only basic loops for now.
Not a duplicate, that one does not enforce recursion. I've opened an issue some time ago over there.
Not to mention the hundreds of issues raised.
it does check the presence of loops though some of the same tests in this one, my point is there are a lot of katas out there that test factorials and some even test the use of any for loops
Can you give me a link of one of them that forces recursion?
the one at the top of this thread enforces recursion the same way yours does, with a few less tests but the base is the same
The one at the top does not enforce recursion. See the issue I raised there some time ago.
Also, that one is very broken and the author isn't fixing the issues, its a dead kata.
because there isn't a codewars guide to what a duplicate is , i guess it's open to interpretation and mine is where i can copy a solution from one kata e.g. the kata above and evevn below and pass all test cases with as little to no change to code.
This proposal is different comparing to other similar katas. I don't think it is a duplicate. If someone else thinks it is duplicated i will reconsider this.
Duplicate http://www.codewars.com/kata/factorial-factory/javascript
This is a series of katas that are supposed to be solved with recursion. You can't solve it in another way.
Choosing factorial and fibonacci again is because those are classical problems to be solved with recursion, so I though it would be an easy way to make people understand it.
It is the same mathematical problem but not the same approach.
Are you sure about that? By the way, very nice kata description design.
Yeah, don't get me wrong it's a great series and I guess having the restriction inforced seperates it from all the other factorial katas. The problem I had is that I could just copy and paste an answer from another kata and pass this one without having to change any code... I guess a second opinion on this regarding what makes a kata a duplicate would be helpful :)
This comment has been hidden.
Matt. That's ingenious. That's actually something YOU, but not the USER can use, if one uses
"use strict";
at the right place. Mind if I add it to the Codex?Why thank you, I would be honoured if you added it to the codex (I must add there need to be improvements to dynamically ignore preloaded code).
This kata got 31 upvotes already and it seems that no one want to enter in this discussion. This kata could be blocked forever.
I think it is better to mark as solved and, if someone also thinks that it is duplicated, even with it's different proposal (recursion) we can reopen it.
Also, about the test cases, I know that this doesn't prevent all hacks, but it does the basic. I'm happy with the results when I see that almost all solutions are following the idea.