5 kyu
Product of consecutive Fib numbers
1,787 of 69,077g964
Loading description...
Algorithms
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.
For C# method name productFib should start with capital letter - ProductFib.
Naah this was wayy to fun I just forgot once that functions cant be inside other functions lol but other than that I found it very intriguing. I also didnt really understand what was wanted from be but I found what they meant in the questions section.
For Python, just a suggestion: the description looks like returing a 3-tuple is expected (having the round brackets: "(21, 34, true)"), and having different types for the elements underlines that. But the tests expect a list with 3 elements. Maybe we should change the description to square brackets?
Why in description there is nothing about final result in list?
It does : "returns an array/tuple (check the function signature/sample tests for the return type in your language".
good, but not 5 kyu, maybe 6
umm... why am i asked to put a boolean true or false into a unsigned long long vector i thought that isn't allowed ???
In C# it expects an array where 3rd element is expected 1 for true, 0 for false, what is your language?
Please change the details description so that F(0) = 0, the F(0)=1, F(1)=1 is highly confusing!
fixed
My code doesn't pass test $this->revTest(productFib(5895), [89, 144, false]); But 89 * 144 = 12816, not 5895... My answer is [45, 131, false], 45 * 131 = 5895. May be I didn't get requirements clear? Or this is a bug in the test?
The test is fine, check your code. 45 and 131 aren't Fibonacci numbers. Not a kata issue, please use
Question
label next time.Yes, 45 and 131 aren't Fibonacci numbers, this is correct, i.e. third element is result array if false (in my result and in test result). Querstion just about numbers... i.e. if no any multiples for both Fibonacci numbers to be equal prod, I have to find: F(n) should be Fibonacci number, less than F(n+1)(and F(n+1) isn't a Fibonacci, and F(n) * F(n+1) != prod. Correct? P.S. And thank you, I got about 'Issue' and 'Question'
Both should be Fibonacci numbers F(n) and the next one F(n+1). Check the examples at the end of the description.
This comment has been hidden.
Your code is too inefficient.
prod
can be very large in tests, which means your fibonacci function will run way too many times. Try testing locally with an input of11848599062691545965064653087608697333062993570318609685770465827303336974
(which is an actual input the random tests generated for me), you should see what I mean.Im sorry, I didn't get the problem. can someone elaborate to me more clearly?
Which part confuses you? Given a number, you have to return which two consecutive Fibonacci numbers multiplied give that number, if there are no such numbers, you should return the smallest pair that when multiplied, gives a number greater than the number passed to your function. About the format of what your function should return, it's specified in the tests.
Lua translation!
fun lil problem, nice iterative solution
I can't understand the conditions. The tests pass and the rest does not. Can you explain?
Test 12 fails with the error arrays first differed at element [0]; expected:<1836311903> but was:<4167823282> Is there a correct check there?
In what language?
Java
Yes, this check is correct, and your solution is buggy. The bad thing is, random tets do not catch this bug :(
*** F(n) being the smallest one such as F(n) * F(n+1) > prod. *** Silly me! My previous solution didn't notice this line of instruction. Be careful!
For C/C++, there should at least be a test case where prod is so big that the value of Fib(n) * Fib(n+1) overflows the unsigned long long. It's an unincluded edge case.
C fork
Approved, will leave description update till later
i got error in javascript
TypeError: assert.sameOrderedMezmbers is not a function at Context. (test.js:43:12) at process.processImmediate (node:internal/timers:471:21
is it error from kata or not??
const { assert } = require('chai');
describe("Tests", () => { it("test", () => { assert.sameOrderedMembers(productFib(4895), [55, 89, true]) .....
I can't say for sure, but it's very likely you've accidentally modified the
Sample Tests
tab (the thing below theSolution
tab). Try to copy your code from theSolution
tab somewhere temporarily, then click "RESET
" at the bottom right of the trainer screen (warning: it WILL discard your solution, so make sure to copy your solution somewhere on your PC first). Now, theSample Tests
should've gone back to their original state. Paste your code back into theSolution
tab and see if it works now.thank you it's work
when solving a java problem, I encounter a problem whose array was initially different in element [1]; expected:<2971215073>, but it was:<1>, although the long type 2971215073 is not suitable. can I remember the type of the returned value?
Not a kata problem.
python new test framework is required. updated in this fork
Approved.
this kata is very interesting, i built my own fibonnaci formula inside my answer but, some people had a way to do this with simple math i didnt undertood how this work, but since my goal inst math but programming, so i think my answer is valid
should this really be 5 kyu? Maybe 6 kyu is a more suitable rank
ranks cannot be changed for approved kata
Test keeps telling: arrays first differed at element [0]; expected:<55> but was:<1836311903> It works correct on my machine. Any ideas? (java)
Try printing out your class variables
FIRST
andSECOND
at the beginning of eachproductFib()
function call. Think about what astatic
class variable is, and how it will work in this context if several tests are run consecutively.As a side note the
issue
tag is for issues that you can prove are with the kata itself, not issues you're having solving it. I've resolved this one; please use thequestion
tag in the future for this kind of query.Thank you.
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
Your function doesn't return a value, so it returns
undefined
as default.Simple return. lol thank you
return kata.sensei === 'g964' ? skip : train;
This comment has been hidden.
You are using a
static
variable. This line:will only be executed once, the first time the function is called. For the next calls
nb
will carry over its value from the last call. As a result, it is not correctly initialized for your algorithm.Great, Thanks!
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )This is a classic problem that can be solved using recursion and I think one of those solutions should be highlighted.
This comment has been hidden.
This comment has been hidden.
am i learning to code or how to do math
yes
I have a problem with that test: arrays first differed at element [0]; expected:<1836311903> but was:<-2133083224>
This test shouldn't even exist
What language are you training? Why this test should not exist?
because the return value should be in an array of three values, and here is only one.
Code in Java
But this is what the error message tries to tell you:
" >>>arrays first differed at element [0]<<<; expected:<1836311903> but was:<-2133083224>"
- it means that you returned an array, but its 0-th element is different than 0-th element of the expected array.I do agree it could be a bit clearer, but still the message does not present "a test which should not be there".
ok, thanks
Not a kata error, all is said.
Hint: Recursive functions are slow as more recursion occours.
My returned values are correct and yet the test and attempt both fail.
return f"[{Fib[i]}, {Fib[i+1]}, True]" Is there something wrong with my return line?
Yes, your function is returning a string, it should return a list.
This kata finally got me to understand the technique were you "remember" values and can't explicitly quote to not give away the solution. Great kata!
I thought there were performance tests for speed...lol guess not then
My advice don't look at details. Look at test cases.
TIP: Try not storing the fibonacci series.
for me this worked, but maybe for doing in C++
Should be a 6 kyu kata in my opinion.
Agree
this kata should be 6 kyu if the math at it was obvious.
Add check for zero test case {0, 1, true}, although trivial, you could create a solution that ignores this possibility and still pass.
The description should be updated with the comment made by @post-rex.
Update JS tests to Node 14.
Approved.
This comment has been hidden.
Hello, you are so close with the code you have here. The trouble is not large input values. For example, consider what happens with input of 10. The trouble is that your logic is slightly off when the product is not equal to prod. Think about why that is and I think you will see it.
OP solved it, closing
This comment has been hidden.
Your recursive method for finding Fibonacci numbers, you build the series from scratch each time you call it.
Closing this one.
This comment has been hidden.
Not an issue.
of course it is. Every cata cretaed by g964 should be 1kyu. If you figure out what he means in his convoluted description, you right away deserve senior position at FAANG company.
This comment has been hidden.
The test is fine, read the description again. Not a kata issue.
Discription: F(n) being the smallest one such as F(n) * F(n+1) > prod.
F(n) ... 55, 89, 144 ... prod === 5895
55 * 89 = 4895 < 5895 89 * 144 = 12816 > 5895 ===> [55,144,false]
Man the directions on this were so confusing. I spent a long time trying to figure it out and then looked at the test cases and realized I wasn't trying to find factors of prod that were fib numbers and returning if they were consecutive of each other or not. Once I read the comment by a random user that clarified what the directions were I got it almost right away.
Update to Scala 3
Also adds some edge case testing which was missing before.
.
I think it's 6kyu at least. It's very easy.
This comment has been hidden.
Can you give an example being wrong? You're most probably mistaken. Please refer to the documentation to see how to post useful messages: https://docs.codewars.com/training/troubleshooting/
I say that the correct values in the tests are set incorrectly why do I send the answer and [17711, 10946, True] should equal [10946, 17711, True] writes to me, and when I swapped them, nothing changes again, an error and for the same reason, the variables are just not like that, although how not to change in places everything will be wrong
I don't understand what you are saying.
[17711, 10946, True] should equal [10946, 17711, True]
means you returned the first array but the correct answer is the second one.Time: 795ms Passed: 28 Failed: 3 Exit Code: 1 28 test passed and 3 failded. I dont understand what wrong?
Your code fails on some tests.
NASM translation
Approved.
Factor translation
Approved
This comment has been hidden.
I don't know, but certainly not raise an issue. Please read the documentation: https://docs.codewars.com/training/troubleshooting/
Did not find answer, but thank you
This comment has been hidden.
So, I have read through the issues section on this Kata, so it would be pointless for me to bring up the same things so many others have brought up because it doesn't seem to be making any sense to the individuals in charge of change with this kata. So instead, I'll try this way. In the kata description there is no way to know to what extent the program needs to run the product. What I mean is that there is no statement of "Your code needs to be able to run x number of digits in the input." It would help prepare people to know they need to be efficient rather than having it sprung on them after working hard to come up with a solution. I hope this helps, and thank you for this kata. It was a lot of fun to work on.
got it finally ; )
how?
Which language? (can only help if it is c++,python or java)
This comment has been hidden.
This comment has been hidden.
Your code fails for cases when
prod
is0
or1
.Read about this
Well, can do true, instructions are kind of vague in the false case
This comment has been hidden.
What's the purpose of your comment? Please mark it with a spoiler flag when you give possible hints to a kata (I mark if for you this time).
Well, I cannot submit my solution because it will not be admitted despite I think it is ok. I looks like the test should be dotest(5895, (55, 89, false)) instead of dotest(5895, (89, 144, false)). If I'm mistaken please could you point me my error ?
Ok, you mean your solution doesn't pass the tests (sorry I didn't understand). Read carefully the description: You must find
F(n) * F(n+1) >= prod
. Here55 * 89 = 4895 (< 5895)
, so it's not ok.Your error comes from supposing that if
fib(n) <= sqrt(x)
andfib(n + 1) >= sqrt(x)
thenfib(n) * fib(n + 1) >= x
. That's wrong. forget fibonacci for a moment:9 < sqrt(100)
,11 > sqrt(100)
, but obviously9 * 11 < 100
.This comment has been hidden.
All you can tell by stopping where you do is if the third element is false or true, but it doesn't always give you the first two elements when it's false (just look at the example you're giving, it's clear the answer is not good)... Well, you're not so far from the solution... I'm sure you'll end up getting it. I mark the rest of the discussion with a spoiler flag since it gives too many hints.
This comment has been hidden.
I figured out what the issue was, so instead of using a template literal, i stored the boolean into a variable
Not a kata issue.
This comment has been hidden.
Please read the description.
You are not being asked for numbers whose product is in the Fibonacci sequence.
Actually, the problem here was that Python returns numbers like 573147844013817084101 in exponential form like so: 5.731478440138171e+20. And this form doesn't == the decimal form, that is why i had an error. So, i changed my code. And my code wasn't actually picking only prods that are in Feb seq. It was in the beginning, but since it was 4 am when i started this kata, my thoughts were probably just all over the place & i thought that the problem was in that the prod was not in the Feb seq, while my code wasn't even checking it:) time for a nap :)
Given prod, you need to find F(n) * F(n+1) = prod.
prod isn't a Fibonacci number. I'll give you an example, let's say prod is 15, not a FN. The consecutive FNs which multiplied give you 15 are 3 and 5.
thanks, already got it :)
Hello, I believe it is a terrible practice to return an array, which consists of multiple types. For example in python, the arguments should be typed - I believe it would be much better if the function returned a Tuple[int, int, bool]
Do you think the kata will be changed when more than 38000 people passed it?
It should be changed. It is even more sad, that 38000 people are getting the wrong practice.
In a perfect world, all kata would be, and would always have been, written according to all best practices.
This is not a perfect world.
( JavaScript doesn't even have tuples, yet people program in it. )
COBOL translation.
approved
So my code runs and executes in 500ms but when I attempt soltion I get an execution timeout( exeeded 12000ms ). I have no idea what the issue could be. My algorithm complexity is not the best but .... I dunno what to do.
Not an issue. If you time out it's because your cde is too slow.
This comment has been hidden.
I did not understand the problem before this explanation. Thank you!
great explanation , thanks .
It is the description which is making this problem seem harder than it really is. The authors may need to revisit and define it properly.
The more I read it, the less I understand what it wants from me.
This comment has been hidden.
This comment has been hidden.
See the sample tests:
It's an array of longs.
This comment has been hidden.
You passed the kata.
I figured it out myself
3 of textcases are giving incorrect results
Which tests? Please state the input values. Which language?
This comment has been hidden.
You passed so I think your answered yourself...
No! that is the problem. When I tried the code in my editor, it returns the desired answer; but in the Codewars' editor, it returns
None
. The one I passed is just a stupid trick. Is there a problem with my algorithm or there are some issue that I need to report?Check what
fibo(0)
returns. It's a problem with your code.This is the problem I have been saying; in my own IDE it works without problem while on Codewars it returns
None
which I can not fix!Please read what I wrote, what does
fibo(0)
return? NotproductFib(0)
,fibo(0)
Thanks !! I got it now.
(Swift) passing all test cases, but when I attempt, I get this error: "XCTAssertTrue failed -" but does not tell me what that "..." does not equal "..." I have no hints as to what is making this fail!
Fixed with this fork: https://www.codewars.com/kumite/58a485202f949ecb3b000033?sel=610bd7e9ed5ee4000e6d99b9
@hobovsky: impossible to approve -> I get "Language Version is invalid for swift" and I don't see a way to change the version (there is no visible version).
Please check again.
@GreenbergKU: for now it is impossible to approve the fork. I tried your solution: your printing is just above the test you fail. I think you stop your fib sequence too soon.
Approved this time, thanks!!!
This comment has been hidden.
Hello guys, I ma having this issue: "Passed: 30 Failed: 1 Exit Code: 1" Is there any special test case I need to consider in my code?
Felt overwhelmed after reading description, but this kata turned to be much easier than expected :) Good one, however I think difficulty rank should be actually around 6 kyu.
Why the change from
n
tom
there?Modified. Why? Maybe because the name of the variable doesn't matter (?)
This comment has been hidden.
Not a kata issue, your code returns the wrong numbers.
This comment has been hidden.
Read the text in bold too. It's not what you think you should return, it's returning what you're asked to do.
For
productFib(5895)
55 * 89 = 4895
and4895 < 5895
, so yes, your numbers are wrong.This comment has been hidden.
I wonder whether '0' should be treated like isolated case. I stuck there, else all tests passed. I guess not the optimal solution.
Thanks for the Kata, it was fun :)
It turned out to be easier than I thought
Hi OleksiiB, I am having problem about time out excution of my code.Is the problem spanning from using the while loop in there.
Sorry, can't see your solution. I suppose you need to post it separately, mark it as question and spoiler. Then, kata author or someone with higher kyu will reply. Don't forget to wrap it in proper markdown.
This comment has been hidden.
Locally JavaScript code works fine (using loop). Test results (console.logged) show some erratic calculations. Checking them locally : code works fine and gives a right answer. Puzzled.
Are you using a global var? Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Thank you for a quick reply! Will investigate!
This comment has been hidden.
Since you call
next
on the generator,number1
is1
instead of0
It is much easier than usual for 5 kyu
True, I thought there would be super big numbers to test on, thus forcing us to come up with a more efficient code.
None should equal [0,1,True].Dose it means when prod==None, it should return [0,1,True]. However, when I code like this: if prod == None: return [0,1,True] it cannot pass the test.
The input is not a list, no. Your error output is telling you what was expected and what your result was. It does not say what the input was.
Thank you!
Excuse me but how could you possibly imagine to put a boolean value such as true or false in a long array??!
If you look at the example tests, they show you.
Good problem. Reminds me of some of the project Euler problems.
However, it would be really nice to have more "typescripty" typescript requirements instead of just copy/pasting the javascript problem and labeling it typescript. I wouldn't have said anything, but I've now encountered a handful of "typescript" problems that are really just javascript and the available typescript versions are several major versions behind. I realize typescript is just javascript... but the whole point of typescript is to make people write better javascript. It's great that it still allows vanilla javascript, but the unfortunate result is that people aren't getting the advantages of typescript.
A concrete example for an issue that could be improved would be how we're returning multiple values of different types from a function, which is a bad practice. If you must do that, create a class for the return type. Or if you're unwilling to do the extra work (though it's not that much work), you could use the project Euler approach and expect an answer that is some composite or computed single value. I just created a codewars account and the intro suggested that people are trying to end up with production quality code, but problem requirements like this force bad practices.
You should author your own TS kata:-)
I'm getting Failed at one test and I don't know why, the Test Case #12 in C# -.-!
Check what is the test input, reproduce it locally, and debug through it.
Thanks, I resolved it.
Same but in Python. I don't know know how to go around it.
This comment has been hidden.
No your code is wrong. The thing printed comes first then the assertion failed sentence. prod = 0 for that instance
I saw it just now, I was looking at the wrong test message the wrong was prod = 0. Thanks!
Start your fibonacci sequence from 0 instead of 1.
This comment has been hidden.
Does Java detect overflow errors for 32-bit signed int?
When a 32-bit int goes beyond 2,147,483,647 it might be flipping to a negative value. A 64-bit long might avoid that problem by having a much higher limit before it rolls over to a negative value.
This comment has been hidden.
Your code is wrong, no unexpected indent when I try your solution. You should read https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
You are timing out because your code is not efficient enough.
Thanks for reply. although i found different solution and its working now. but still thanks !!! im just a beginner so it's taking time to make proper logic but i'm glad i'm making progress ...
This comment has been hidden.
And did you try not using a library for multiplying two numbers?
Thank You! It worked when i multiplied it directly. So, numpy uses C type variables something and Overflow occurred. [found on stack overflow]
you got to think cleaner pal, all the solutions that i have seen in this forum are sooooo complicated compared to what i did. i'm not yet at the point where i can say if my solution is the best or if it's efficient, but sometimes i can do simple stuff for sure.
Although overflow isn't a problem for Python, it appears to be a problem for the numpy package:
https://numpy.org/doc/stable/reference/generated/numpy.prod.html
"Arithmetic is modular when using integer types, and no error is raised on overflow. That means that, on a 32-bit platform: > x = np.array([536870910, 536870910, 536870910, 536870910]) > np.prod(x) 16 # may vary"
This comment has been hidden.
I've found this with the Java solutions as well. A test for
Long.MAX_VALUE
fails for many of the solutions and the description doesn't specify an upper limit forprod
.exit code 132 on Swift with matching/passing test cases
Same for me
Guys this did not feel like a 5 kyu kata at all
What do you mean? Was it too easy or too hard?
I thought for a 5th kyu kata it was pretty simple. There's plenty 6th kyu katas i found much more challenging. It's just my personal opinion though
I agree. As a beginner to codewars, I have found some 6th or 7th kyu katas much harder. Though maybe they were just covering uses of python that I haven't been exposed to before.
This comment has been hidden.
I think it depends a lot on the methods you are familiar with. A few weeks ago I found this really hard, then learned a new method and completed it in few minutes.
The main test exits with "Time: 2668ms Exit Code: 132"
Not an issue of the kata but of your code. 130 guys passed the Swift (I suppose it is your language...) Kata. I tried a few solutions without any problem.
Okay, I'll try different algorithms then.
Python. It's kinda weird to enforce the return type as an array. One should just check that the result is an iterable of the right size with the right elements, there is no reason to reject tuples or any other Iterable.
This comment has been hidden.
Post your code using markdown formatting and marking your post as having spoiler content so we can help you.
This comment has been hidden.
You're doing recursion wrong. You need to return the function call too.
I feel silly now *facepalm *.
Thank you!
The function name should be in snake_case in Python and Ruby.
bruh wrong kata...
done for python
done for Ruby
This comment has been hidden.
"Exit Code: 132"
My test results are correct but I get "Exit Code: 132" and can't submit. Im in Swift
Even I am having this issue.
This comment has been hidden.
Until now a table of pre-calculated values has never been considered as cheating. I will be happy if you feel like adding such tests:-)
From point of view, it IS cheating. Because the point of the kata is not only finding out an algorith, but also implenting it in a particular language.
Unfortunately, I don't have the resources to refactor solutions in so many languages, but probably someone else has. I just ask not to close this issue.
Most of the time you need to find the algorithm to fill the pre-calculated table unless there are very few tests or you have the patience to attempt the series of random tests one by one...
Neither do I.
This comment has been hidden.
This comment has been hidden.
@FArekkusu: Well... all this would have been constructive in beta phase but now it demands too much time for one only man (except, maybe you:-) and 29 languages.
done for python.
This comment has been hidden.
I am sorry but I don't understand what you mean. Maybe you could be more explicit?
This comment has been hidden.
This comment has been hidden.
Yep, that's how I noticed it, in the first place. :)
Well, leaving the issue open is not the worst case scenario.
This comment has been hidden.
Oh that's very cool, thank you very much!
Can't pass one test it responds:
or
depending on what output my program has.
Python works fine. (I'm assuming you're using Python as it's the only language you've trained in so far.)
If you have a problem with your code, it's better to post it as a question rather than as issue (as the issue tag is reserved for problems with the kata).
This comment has been hidden.
'alr approved some time ago'
Attempting in C. Can't figure this one out at all. This function in the test code:-
char* array2StringULongLong(ull* array, int sz);
seems to be mangling my returned array so that I get errors like this...
Error. Expected 55, 89, 1 but got 55, 89, 4201070
when I am certain that the third item is the array is 1, not 4201070. I have tried using the bool lib and using true, I have also tried using littorals with various suffixes, can't get it to work. Help pls.
Are you returning what is asked? (re-read the signature of the function).
yes, i'm returning a fixed size array of ull (ull ret[3]) which should be equivalent to an unsigned long long*
ah ok, I wasn't allocating space for the array correctly (with calloc) so the Array2String... was showing the garbage values in my uncleared memory.
Wow this was really tough for me, I'm not used to doing math like this.
Really needs more example tests for C#, there's only one positive outcome at a relatively low range, not very useful at all.
I'm making a habit out of commenting when a kata's difficulty is not matching its rating but...
This is definitely a 6kyu, not a 5kyu like it is rated currently.
Ranks cant be changeed
It will be great if there are tests for large numbers:)
My solution passes all the tests up to very big numbers in Ruby
Is there any way to add the number it passes to the test in the output so I can verify if it's my code or the Kata :P
This comment has been hidden.
This comment has been hidden.
Hello, Please reply me if any of you have time. I'm facing only one error: exitcode 139. The program is run well in IDE but not in the web. I did my own searching but nothing works. Is there anybody knows how to fix it? Can you do me a favor to help? Thanks
Segfault. Also, not an issue.
Thank you ! I am trying to figure it out. <3
Wrong solutions, bad tests on C#: infinite cycle when prod = ulong.MaxValue for 'Best Pricatice' solution
This comment has been hidden.
Have you tried with a loop?
This comment has been hidden.
I really can tell much more without spoiling the solution, read what you have to find in the instructions, think about how are Fibonacci numbers changing and it'll come eventually.
Nevermind, I figured it out, I was over complicating it. Loop was the solution. Thanks for the advice!
So I managed to finish this one, but I'm confused why it says I failed. The first data set is asking me to get 55, 89, true, which I do, and even in the log it shows 55, 89, true, and I altered the string to display exactly '[55, 89, true]' in the log, but the compiler states that it recieved "undefined", is it not wanting me to log the results? Does it want the data to be stored in specific variables? I am very confused as to why this is happening when I've fufilled the requirements
Your function should
return
that value. Are you usingconsole.log
instead?about 5 minutes after posting that I realized. kind of the whole point of the intro question lol. thanks for the quick reply
i deleted this comment never mind
Even with single line in function (return 0) gives me this error : "This error was caused due to an issue processing the web request, not because of an issue executing your code. You can retry the request." Also i see no errors on https://codewars.statuspage.io/ :/
Language?
JavaScript. But it works now! Thanks for fast reply.
It is unfortunate that you require the nearest pair or else there is an elegant algebraic solution.
Basically, it seems like you approached it algorithmically and got a nearest pair from your algorithm, so you just added it as a requirement for the return value.
This comment has been hidden.
That is common, the final tests are often MUCH more demanding.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Did you read this part?
Yes. But I see the bit I missed now:
"F(m) being the smallest one such as F(m) * F(m+1) > prod."
I was confused by being told return F(m) when there was no m that worked, I didn't realise I had to read further before it made sense.
OK. Good.
Kind of overrated.. It seems not as hard as many other 5 kyu katas.
Try to use a paper for make some maths and find a better algorithm. Don't forget the correct data type.
This comment has been hidden.
Just in case anyone is struggling, as I was, with runtime despite setting an upper bound. Not recursively defining my fibonacci number function did the trick.
btw if someone managed to succeed with recursion (in python), pls let me know
Never mind about this comment. I don't think memoization with recursion works you have to use a loop.
I'm the only one with bug this kata in c#
i can't pass the test 13 and i dont know why cause all works.
This comment has been hidden.
Using the closed form expression of the Fib sequence to calculate fib numbers gives one off the true value for certain values, resulting in an incorrect output when using it.
Eg According to the expression, F(76) = 3416...706 However, F(74) + F(75) = 3416...707
Why is this? Im using Haskell btw.
This comment has been hidden.
It is a question, not an issue of the kata. 263 guys passed the Haskell kata. Can be a temporary Codewars problem.
Liked it, though ranked somewhat high IMHO. Thanks g964 for this one!
It's not the author who ranks but a moderator.
Is it possible to rank the solutions by their speed? Or memory usage?
I think mine may be the fastest of all - how can I verify?
Oh no, 'tis me that is the fairest of them all !
This comment has been hidden.
Different language versions has test cases of vastly different sizes (and hence vastly different difficulties).
e.g Python has test cases with 42 digits, while the biggest test case in JS only has 9 digits. Then for some languages the limit is 64-bit long.
Language versions which does not support BigInts should be cut off (deleted) from the kata. (Or I guess you can limit the number to just Int32 range, but then it'd be a
7kyu
.)Most of the languages (Java, Python, Ruby, JS...) were already there when Jake approved the kata. Your post confirm what I have already asked for : different kyu for different languages. It would be necessary not only for the solvers but for the authors too. Authoring a kata in C - for example - is often much more difficult than in Python or JS... I think that the difficulty (if there is difficulty in this kata) is not in the number of digits; when you have big ints at your disposal or a language such as Python or Ruby or some others coding is not more difficult than in languages without them. Anyway the kata is soon 3 years old... PS: Are you trying to break a record for the number of posted issues?-)
Well, then at least Python/Clojure/Ruby shouldn't have those 2 test cases with numbers over 40 digits. Or C/C#/F#/Go/Java/etc where the range of the input is
long
and notint
. There are currently 3 kinds of tests with completely different input ranges.We can worry about the actual ranking later, but the translations should be as consistent with each other as possible, otherwise the kata is all over the place in respect to the language versions.
(Also, technically someone has to raise the issues at the old katas for adding in random tests/making translations consistent. They have to be done in some point anyways, they can't just be put under the rug; that'd mean a pile of hot garbage. You can consider them the technical debt of CW I guess.)
I can understand your position. But : with their easy big numbers Python (2499 solutions) /Clojure/Ruby (602 solutions) are not more difficult than JS for example; same with C/C# (588 solutions)/F#/Go/Java (1011 solutions)/etc where long or uint64 or u64 are as easy than int. In languages with function signatures I can't change without invalidating already given solutions, which I don't want. I repeat myself: here the thing is the coding, not the number of digits. I think I can close this issue though I will be sorry if we disagree. Still, you can reopen it if it please you.
Last remark: lot of old katas don't respect the supposed high current CW level (look for example at lots of Jake's kata: no "Sample tests", sometimes no random tests) but history is history: You can't remake old battles... Let us keep old katas as archives.
It was you who wrote the language/approved the translation in the first place. So it was your fault. Besides, you never specified the input range, and different input range can, and will completely transform a kata. You need to accept that you sometimes slip up and there are issues that should be left dangling because they are issues that are not resolved. Sweeping them under a mug by resolving them does not make them go away. They never are; they're just waiting for someone else to dig them up (which is mostly what I do after plowing through the beta hot garbage). You can't maintain kata quality without people pointing at what needs to be fixed/improved.
Your attempt at keeping your katas rid of unresolved issues is so ruthless it's frequently frustrating for others to interact with you about your katas. You always speak like you care about resolving the issue status rather than any potential actual problems. Like, do you see how many people say the kata descriptions are hard to understand, and then you just keep reiterating "kata description is clear" without even asking what is being unclear? That's a incredibly closed-minded and self-centered way of handling your own katas. It's not a mindset a kata author ought to have at all.
Also, have you considered that they also help other people notified about potential pitfalls too? You never specified your input range after all, so nobody can know the performance requirements until they throw themselves at the actual test cases.
By the way, in case you don't know already there are plans to release the 500 solve limit some time in the near future, which at that point it'd be a ongoing project to fix all these technical debts. This is why I've started raising issues on older katas, it makes them easier to spot out among thousands of old katas.
great kata, don't know why people are complaining. I pretty much know what to do from reading the title.
You gave very big numbers in the attempt, codewards doesnt allow the code to run for that long to find that big number.
This comment has been hidden.
wtf is this shit lmao the guy who wrote this can't even ask a coherent question.
The solution is really simple.
This comment has been hidden.
When you ask for help don't post an issue - you make the author lose points - post a question. As you could have seen at the top of the page 6319 guys passed the kata (1945 with Python) so there is no error in the tests. Google "UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' surrogates not allowed".
It is too simple for 5 kyu. It should be 7 kyu not higher.
It's not the author who gives the kyu but a moderator and there should be different kyu for different languages. Furthermore yo are certainly too good:-)
Though ull is enough,but guess the limit of one problem is not a good habit.So i think it should be marked on the introduce of this problem.Anyway this problem is 5kyu,and is easier than it should be.
This has to be the worst one yet I've tried in C++, no matter what I tried it somehow refused to properly return my array.
No problem with the C++ kata. As you could have seen at the top of the page 254 guys passed the C++ kata (out of a total of 5070).
Haskell. The problem is that fibs implementation used in tests is quite inefficient and despite the fact that my solution forks locally and works fast, testing eats up the whole time slot. That's unfair, I believe. fibs implementation used in tests must use memoization or whatever, otherwise it affects the execution significantly.
As you could have seen at the top of the page 198 guys (out of a total of 5059) passed the Haskell kata. I tried many different solutions and the mean time is about 3000-3300 milliseconds though CW seems slow by now. The mean time is far away from 12000 ms so there is no unfairness. Maybe you could have yourself a lighter implementation of fib which could be faster; who knows?
So, my local machine is faster than CW servers?
And.. 198 out of 5059 - is it godd ratio?
CW servers are not that regular... and local machines are understandably faster than far away servers that have a very heavy load. Yes 198 (Haskell) out of 5059 (for all languages) is a very good ratio for Haskell!
Every single kata by this author has been horrible so far. Information missing every time. This one had to return a static array or nothing worked (go figure). Malloc'd stuff would break everything up. Terrible.
This comment has been hidden.
horribly worded kata...can this not be a 'training kata'? just reading the task description put me to sleep...worst kata i've encountered. skip this one.
I can't seem to get TypeScript version to work. No matter what I change in the solution, I get
../home/codewarrior/spec.ts(1,1): error TS1084: Invalid 'reference' directive syntax.
Unfortunately it is a usual bug at CW which doesn't save correctly the kata. Fixed, thanks!
Thanks for the quick reply. I am still getting the same error. I am also getting the error on your Tortoise Racing problem. You are able to test and submit solutions for these problems? Edit: I think I can submit but not test.
I tried both with a few solutions and had no problem. Tell me if it works or not. Don't forget to refresh the page.
I passed 13/13 java tests and it said failed 0 but there is no SUBMIT button. The same goes for my C solution, why is that?
Currently Codewars platform has lots of issues. It's not a problem of this kata.
https://github.com/Codewars/codewars.com/issues/893
/python 3.4.3/ When i run the examples everything is fine (takes ~100ms to finnish), but when i try to attempt:
Time: 2175 ms Passed: 0 Failed: 0 "Response received but no data was written to STDOUT or STDERR. SIGKILL
Process exited prematurally with SIGKILL signal."
hmm, lol? should i wait?
Or this: "Server Execution Error: The server timed out waiting for the code to finish executing. It is possible that this is due to high server load. It may also be caused by inefficent code. Please try your request again." The SIGKILL is still weird....
As you can see at the top of the page 1143 guys passed the Python kata, so the problem doesn't come from the kata. Maybe a flaw in your code or a CW problem. In these two cases I can do nothing for you, sorry:-(
True!
I have the same issue. Did you figured out what was wrong?
The test is pretty bad designed, author only answering "X" guys passed, your fault at each point bringing that out really doesn't help.
It really should have at least one test with a large number to predict the time allocation for this question.
Questions like this one are the ones that prevent Code Wars experiences being all around pleasant
In examples True is written as true
Depending on the language one has "true" or "True"...
sorry. I forgot problem was for more than one languages
AC in one go cake walk!!
Completely disregarded the phi nonsense as it is not needed. The fib sequence they want you to use is actually 1,1,2... instead of 0,1,1,2,.. as provided in the example. This got me stuck for a while.
The description says about the phi nonsense:
Moreover you are wrong somewhere:
the fib sequence needed is 0, 1, ...
This comment has been hidden.
The problem may have been a tad easy for 5 kyu, but I enjoyed looking through everyone's Python solutions. Many used memoization. Some dude even made a memoize decorator. I saw use of dynamic programming and generators, too. Someone new to these techniques can learn a lot. It was a good challenge.
Thanks for your post!
Nice one but seems a bit easy for 5kyu. It may be more intersting with huge 'prod' values though:)
It's not the author who gives the "kyu" but a moderator.
I think the hint at the very end is distractive and lead me to non-efficient solution.
Tried recursive, non recursive method, still getting this error...
Process was terminated. It took longer than 11000ms to complete
Yesterday CW was a bit down, try again:-)
This comment has been hidden.
It is not the author who gives the ranking but a moderator + there are lots of languages in this kata and they are not all with the same problems and difficulty. Moreover maybe you are among the best:-)
It was not my intention to critize the author. Although I appreciate the potential praise, it was rather meant in a relative way. I had a similar experience with another Kata and I just found out you wrote it too. It seems you have an excellent talent for writing Kata's which are really difficult in many languages and are easy in C++ ;). Thanks for making the Katas, appreciated them nevertheless!
I was also wondering why is this 5 kyu. I don't know about C++ but in Python it's trivial :) Anyway, I like this kata :)
I don't like the fact that on 'submit' are executed other tests that are not part of the requirements. If the author of the kata want to have for example productFib(100000000) in less that 1sec this should be part of the requirements.
Probably my code sucks because on 'submit' I receive a timeout error, ok... but how far is my implementation from the requirement? How I'm supposed to know without any clear requirements?
as a developer we always complains that we do not receive clear instruction from our customer about what we want. At least here can we, please, be precise with the requirements?
One should always plan the worst:-) Ok, I am teasing you... Please which language are you using?
eh eh :) the "worst case" doesn't exist :) You can always add a test for calculating f(n^n^n^n) in less than 1ms :D
I'm using ruby. I found a solution to this kata, not the smartest one but it works.
Good "golden ratio"! I'm happy that you succeeded!
I'm happy too :)
Haskell: It's not critical, more to be aesthetic, but since the return type is (_, _, Bool), it would be better to replace
false
withFalse
in description:Aesthetics corrected, thanks:-)
What? Getting error from the haskell test.
/tmp/haskell11563-18-fmwhrh/Codewars/Kata/Fib/Test.hs:17:43: No instance for (Arbitrary t0) arising from a use of
property' The type variable
t0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Arbitrary a => Arbitrary (Blind a) -- Defined inTest.QuickCheck.Modifiers' instance Arbitrary a => Arbitrary (Fixed a) -- Defined in
Test.QuickCheck.Modifiers' instance (Integral a, Bounded a) => Arbitrary (Large a) -- Defined inTest.QuickCheck.Modifiers' ...plus 37 others In the expression: property In the second argument of
($)', namelyproperty $ \ (Positive n) -> productFib n
shouldBesolution n' In a stmt of a 'do' block: it "should work for random numbers" $ property $ \ (Positive n) -> productFib n
shouldBe` solution n/tmp/haskell11563-18-fmwhrh/Codewars/Kata/Fib/Test.hs:18:20: No instance for (Eq t0) arising from a use of
shouldBe' The type variable
t0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Eq a => Eq (GHC.Real.Ratio a) -- Defined inGHC.Real' instance Eq time-1.4.0.1:Data.Time.LocalTime.LocalTime.LocalTime -- Defined in
time-1.4.0.1:Data.Time.LocalTime.LocalTime' instance (Eq e, Data.Functor.Classes.Eq1 m, Eq a) => Eq (Control.Monad.Trans.Error.ErrorT e m a) -- Defined inControl.Monad.Trans.Error' ...plus 210 others In the expression: productFib n
shouldBesolution n In the second argument of
($)', namely\ (Positive n) -> productFib n
shouldBesolution n' In the second argument of
($)', namelyproperty $ \ (Positive n) -> productFib n
shouldBe` solution n'Can't reproduce. Did you relax the types of your solution?
I thought you were on holidays!-)
Oh yeah, I did figured out I commented the function signature.
I'm getting OverflowError: Python int too large to convert to C long. with productFib(256319508074468182850). That's because xrange() requires its arguments to fit into a C long. I'm manage to work around using itertool.count(1) and break but it seems annoying.
I think xrange is deprecated... Try something else:-)
Using range instead causes MemoryError. Which is expected since xrange is a generator making numbers on the fly while range makes all the list and store it in the memory.
Many people passed the kata, maybe you have to see again your algorithm. There are some very big numbers:-(
I passed it too. But I guess the way I was solving it was unorthodox. The solution was simplier than I thought. :)
That's one of the reason why I love codewars, we start with a problem, solve it the best way we could, just to find that someone solved it in a better/faster/pretty way.
Thanks for the feedback!
This comment has been hidden.
In the instructions it says:
if you don't find two consecutive F(m) verifying F(m) * F(m+1) = prod. F(m) will be the smallest one such as F(m) * F(m+1) > prod.
productFib 800 -- should return (21, 34, False), -- since F(8) = 21, F(9) = 34, F(10) = 55 and 21 * 34 < 800 < 34 * 55
Is it just me or is this not consistent? (21, 34, False) is < 800, not > 800.
Please, can you tell which language? I don't see test cases with "800" except, maybe in random Haskell tests.
In Haskell, in instructions, under Examples.
@bkaes: please, can you have a look at this problem?
Sure, you are right, a bad copy and paste I think, I corrected it. Thanks!
I'm getting unknown error on submission :( But tests pass, in python...
In which language do you have problems since you pass the Python tests? "Unknow errors" aren't bound to the kata but to problems with Codewars when too busy. It's not an issue for the kata.You can make a bug report to Codewars.
Sounds good, I meant local tests pass in python.
This comment has been hidden.
@ChristianECooper: Lots of thanks, I'll correct it with len(someFibs - 2) since randint(a, b) returns a random integer N such that a <= N <= b and I'm taking 2 consecutive elements.
Haskell translation kumited.
Approved.
Maybe it's a good idea to add test cases like 5456077604922913920 (i.e. with error by 1 on big numbers)?
I thought I have done that, I must have a look... but now I'm largely behind my schedule so I have to wait a bit. Thanks for your feedback and your ideas of tests.
Done!
JS translation kumited.
prod
doesn't exceedNumber.MAX_SAFE_INTEGER
(ie 9007199254740991)Good to know, I'll wait with a Haskell translation to avoid the merging stuff.
JS translation approved, thanks!
@bkaes: Look at my C# translation, there are random tests (imitated from ZozoFouchtra but it's a start, isn't it?). I also added random tests to Ruby.
I've just noticed what's strange in almost all your katas, compared to the ones I've seen when I started with Codewars: you don't provide language specific examples. This makes it sometimes harder for users to translate the kata into another language, since language specific code cannot be used to show better examples.
I strongly suggest you to exchange
productFib(714) --> [21, 34, true]
with the usual Codewars example style:Furthermore, this enables users to address they language specific return types. For example, in Haskell one wouldn't use a list, since lists aren't heterogeneous. So one would use a tuple or a specific other type instead and could show this in the description:
My strange descriptions come, I think, from a (heavy) mathematical background and a light computer one. The math formalism is difficult to use in Codewars but the advantage would be that it is more general that the formalism of any particular language. By the way I saw that I left in my solution a parameter which is a residue of a previous version! I notice furthermore that Codewarriors don't seem to particularly appreciate mathy katas, am I wrong? In your kata "What's a Perfect Power anyway?" I saw lots of brute force solutions and few or none using the gcd of the exponents resulting from the decomposition of the given number into prime factors but I must say that I didn't read all the solutions! I will try to modify the description of this kata as you suggest.
The generic user doesn't have a heavy mathematical background. Note that it's ok to be generic during general description. But IMHO examples should be language specific if it's possible that the syntax differs somewhat. Also, I don't like the current code-based representation of mathy stuff. LaTeX would be great.
I've studied Mathematics and write mostly Haskell. Why do you think do I critize so many descriptions until they're completely well-defined? ;)
1)I didn't speak about you but in general. 2)I modified the description. Hope it's better and clearer. 3)Your definitions above are very elegant and I like that. Unfortunately I don't know Latex!-(