4 kyu

Exponentials as fractions

51 of 527g964
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • Just4FunCoder Avatar

    This comment has been hidden.

  • saudiGuy Avatar
  • saudiGuy Avatar

    This comment has been hidden.

  • ahmet_popaj Avatar

    Hopefully I'm going to solve more and harder ones like this one even though I've got to say this particular kata is basically entry level.

  • Yushi.py Avatar

    I guess I could have saved myself about 30 minutes had I properly read the description and realized that the digits are talking about the numerator and not of precision.

  • hamilton-bonds Avatar

    This comment has been hidden.

  • Quark Fox Avatar

    Thanks g964!! this is the first kata that made me learn a new module!!

    for others, yesterday, i spend more then 4 hours doing this kata without reading the whole instruction!! questining the kata questining myself, i did try all in my way to manuver decimal, created 5-6 extra function, but all they could return was the correct result for the whole numbers, then i read the instruction again the kind auther mentions a method, and i learned from the discourse there is a fraction module.............. thats it, the kata is actually very simple.

  • Quark Fox Avatar

    expand(1.5, 10) --> [36185315027,8074035200]

    is the result okay?

    because numerator has 11 digit, i get 39070599481 by 8717829120 and if its 10 digit, i get 2146736219 by 479001600

    i know many people has solved this kata, so this should be alright but i cannot find my fault??!!

  • Krillan Avatar

    Useful kata, I acquired new knowledge, thanks to the author. For me, it was more difficult than many other 4kyu because I had not used the Rational class before and therefore at first tried to solve it in not the most optimal ways.

  • benjaminzwhite Avatar

    This comment has been hidden.

  • FArekkusu Avatar

    Python 3.10 should be enabled. If you do that, please remove your kata from this list.

  • Unnamed Avatar

    As it was previously noted, some tests with floating point values are incorrect. For example, Python:

    1.6 10
    [5854679515581645, 2251799813685248] should equal [27425286391, 5537109375]
    

    1.6 is 3602879701896397/2251799813685248, the next value after 1/1 in the series is 5854679515581645/2251799813685248.

  • Walter Liu Avatar

    No random tests...

  • lutre69 Avatar

    This was waaaay too easy for a 4 kyu. I usually spend at least 4 hours to solve a 4 kyu. This took me 15 minutes.

  • mauro-1 Avatar

    When x is a float, the value to use is not the same that is passed to the function.

  • hobovsky Avatar

    Why is so many kata designed around so bad handling of floating point values?

  • NandishPatel Avatar

    This comment has been hidden.

  • NandishPatel Avatar

    At first, I was happy that I solved it(kata of 4kyu) pretty fast. Then I looked at the comments and found out how easy it was. Now, I am not happy anymore.

    on to the next one from g964 👍.

  • ziereis Avatar

    Definitely too easy for 4kyu. Wording of the kata could be better but ok.

  • daschne8 Avatar

    Using Ruby the Rational function doesn't work correctly for certain numbers i.e. Rational(1.6) will not give (8/5) unless you first convert it to a string. This caused me a huge headache.

  • Donghao Ying Avatar

    I think this kata is relatively too easy to be ranked as 4 kyu.

  • maxchiodo Avatar

    another piece of crap test. Bye bye codewars

  • maxchiodo Avatar

    Lots of unstated assumptions. Is base 2 exp, base 10? What coefficient? I'll assume 1*10**x but it should be explained.

  • gordon0728 Avatar

    This comment has been hidden.

  • ddellacosta Avatar

    So, I'm confused by something basic in terms of what this is asking, as I don't even understand the first example given. Hopefully by walking through my thought process, someone can spot my error and clarify for me:

    Taking the first example, expand(1,2), the answer we're given is 65/24. If I do the math by hand I see I can get this sequence of fractions by stopping at n = 4:

    1/1   + 1/1   + 1/2   + 1/6  + 1/24
    

    which when summed gives me the result provided in the example:

    24/24 + 24/24 + 12/24 + 4/24 + 1/24 = 65 / 24
    

    So, assuming I've got this much right, if we are supposed to return a value as soon as we calculate a numerator with as many or more digits as specified by the second argument, in this case 2, shouldn't the answer to this actually be 16/6, given by expanding out only to n = 3?

    1/1   + 1/1   + 1/2   + 1/6
    6/6   + 6/6   + 3/6   + 1/6 = 16/6
    

    Considering I'm the first person to mention this I can only imagine that my understanding or calculations are somehow mistaken. Any help would be appreciated--thanks!

  • stecue Avatar

    This comment has been hidden.

  • Lenau87 Avatar

    I get [6976892759,1556755200] for exp(1.5,10) instead of [36185315027,8074035200]

  • TommyX Avatar

    This comment has been hidden.

  • nykros Avatar

    There is an error in the test. The function is specified to use Double for the input of the value of the exponential. The number 1.2 has no exact representacion in IEE-754, so it is aproximated (I guess to 1.2000000476837158203125). So, in Haskell 1.2 is 5404319552844595/4503599627370496. The output of my function is correct.

     should return expansion for number 1.2 and 3
    expected: (401,125)
     but got: (9907919180215091,4503599627370496)
    
  • TommyX Avatar

    This comment has been hidden.

  • ChrisSchwartz Avatar

    Using Python, I got through the first 9 test cases okay but I'm stuck at the x = 1.85, digit = 60. My answer for the numerator is 60 digits long and starts with 502.. divides out to 6.3598195226 (so, a close approximation). I can only assume, given that others have completed this in Python, that I'm doing something wrong and my algorithm is somehow "cheating" on this particular test case.. has anyone else had the same result for this 10th case?

    Thanks!

  • Blind4Basics Avatar

    Well... It's nice to get the points for a 4 kyu but... Why is it 4 kyu??? :o (I'm completely stuck with some of your other mathematical katas that are 6 or 5kyu only, so I'm a bit surprised to get this one in less than 20 minutes! ;) )

    'Was approved with another language? (maybe you shouldn't have had translated it to python?)

    Anyway, just a question, no importance... (and thanks for the points! ;) )

    (PS: java version on it's way ?)

  • bootkeen Avatar

    This comment has been hidden.

  • albertchendao Avatar

    This comment has been hidden.

  • maion Avatar

    This comment has been hidden.

  • daddepledge Avatar

    Enjoyable, thanks.

    This is about a 6 kyu in python. I take it that the 4 kyu grade is correct for another/other language/s - is it that you can only have one grade for all translations?

  • JohanWiltink Avatar

    Doubles and fractions don't mix. How am I supposed to do calculations with 1.5? I know it looks like 3/2, but for all you or I know it might really be 30000001/20000000 (well, add a couple more zeroes).

    I could work with a rational, but I honestly don't know what to do with a double. It's not an exact number.

  • finsternacht Avatar

    Exactly like others said, in the current state this makes not much sense.

    1. Change the description and ask to return the nth tailor series expansion, where the numerator is at least "digit" digits long. Or change the test cases so that they expect the numerator is exactly "digit" digits long.
    2. Explicitly allow x to be a float in the description, otherwise people will, imho correctly, assume x to be an integer. Or exclude the test cases where x is not an integer.
  • antonm Avatar

    Like other comments say, you should include an example using non-integer x in the program description.

    In general, I don't think that allowing non-integer x makes the problem any more interesting. The integer version basically uses the same steps and is a lot easier to understand.

  • bkaes Avatar

    The Haskell version should use a pair instead of a list:

    expand :: Double -> Int -> (Integer, Integer)
    expand x digit = error "to do expand"
    

    Also, Int is enough for expand. You don't want more than 2 million digits, right?

  • bkaes Avatar

    Write exp(x) or exponential(x) instead of plain text "exponential(x)" (you can use a whole lot formatting in your post in general). Even better, write ex.

  • sergeyn Avatar

    This comment has been hidden.

  • acaccia Avatar

    This has already been asked many times, but I still don't understand why expand(1,5) has 6 digits in the numerator. It doesn't fulfil your requirements.

    For expand(1,5), I found [98641,36288], and it seems a really good estimation for me…

    Could you please precise why expand(1,5) should have 6 digits ?

  • Bodigrim Avatar

    This comment has been hidden.

  • jolaf Avatar

    Thank you, very nice kata!

    Now I know that Python Fractions module contains something else besides gcd() function . :)

  • jolaf Avatar

    Why for expand(1, 5) the [109601, 40320] is considered a valid solution while [37441, 13824] is not?

  • Anseltis Avatar

    Why is expand(1,5) = [109601, 40320] although 109601 (numerator) contains 6 digits? exp(1) has continued fraction (see Wolfram Alpha) [2;1,2,1,1,4,1,1,6,1,1,8,1,1,10....] [2;1,2,1,1,4,1,1,6,1,1,8,1,1] = 49171/18089 [2;1,2,1,1,4,1,1,6,1,1,8,1,1,10] = 517656/190435 IMHO, [49171,18089] is more appropriable for expand(1,5) If you want irreducible fraction only Taylor series then this moment must be designated in the description. Also exp(1.85) = 6.359819523 but expand(1.85,60) = [ 1255640015507986459344754396106984611112931890102125595005801691, 205688069665150755269371147819668813122841983204197482918576128]. 125...1 / 205...8 = 6.1045 Nearest irreducible fraction of Taylor series is [ 17212490183856113080811676174541242934582010358766327687597249 2706443181710666438004021657600000000000000000000000000000000].

  • taw Avatar

    First, problem as described makes no sense if x is a float, as it is in test cases.

    Second, what is this supposed to do if results are 3 and then 6 digits when it's asking for 5?

    Third, description is super confusing.

  • devonparsons Avatar

    I have no idea what this kata is asking for.

  • mortonfox Avatar

    It may be worth pointing folks to the power series expansion for ex, in case anyone is unfamiliar with the math:

    http://en.wikipedia.org/wiki/Exponential_function#Formal_definition