5 kyu

Going to zero or to infinity?

869 of 6,163g964
Description
Loading description...
Mathematics
Algorithms
  • Please sign in or sign up to leave a comment.
  • tobeannouncd Avatar

    Haskell fork

    • Test suite updated to use approximate equality instead of exact equality to a truncated result (addressing this issue)
    • Console logging removed from random tests
  • Harman Deep Singh Avatar

    This code dont handle well for Java langauge

  • marwanwae Avatar

    Unfortunately the code I created using go experienced a time out

  • Lilly-the-Witch Avatar

    This comment has been hidden.

  • James-Oswald Avatar

    Kata is nearly unplayable in Clojure due to strict equality checking. Please fix.

  • jackkey2059 Avatar

    wrong test missing import

  • _Anduril Avatar

    This comment has been hidden.

  • depial Avatar

    Julia fork switching to approximate equality with tolerance of 1e-6

  • Cucumberry Avatar

    For rounding issues with R, you don't want to round, you want to throw away everything after the 6th decimal.

  • trashy_incel Avatar

    Some languages still use rounding + strict equality instead of approximate equality with no rounding:

    • Clojure
    • Crystal
    • C#
    • Go
    • Haskell
    • Nim
    • Reason
    • Scala
  • trashy_incel Avatar
  • AMAKURO Avatar

    The Attempt throw me this error: "expected NaN to equal 1.005025" but all good with the tests, and the console.logs, the problem is when i try to return the result

  • Param_1182 Avatar

    This comment has been hidden.

  • ejini战神 Avatar

    C#: method name should be PascalCase (Please refer to implementation of backward compatibility here )

  • vbccbz Avatar

    Fixed__Tests Log Correct, but C++:

    UndefinedBehaviorSanitizer:DEADLYSIGNAL ==1==ERROR: UndefinedBehaviorSanitizer: FPE on unknown address 0x000000428831 (pc 0x000000428831 bp 0x7ffefa15a0d0 sp 0x7ffefa159c50 T1) ==1==WARNING: invalid path to external symbolizer! ==1==WARNING: Failed to use and restart external symbolizer! #0 0x428830 (/workspace/test+0x428830) #1 0x42702e (/workspace/test+0x42702e) #2 0x426a5d (/workspace/test+0x426a5d) #3 0x42674b (/workspace/test+0x42674b) #4 0x42a925 (/workspace/test+0x42a925) #5 0x425f3d (/workspace/test+0x425f3d) #6 0x7fd145f8bc86 (/lib/x86_64-linux-gnu/libc.so.6+0x21c86) #7 0x404619 (/workspace/test+0x404619)

    UndefinedBehaviorSanitizer can not provide additional info. ==1==ABORTING

  • denartha10 Avatar

    Hi. There is an issue with the TypeScript/JavaScript version of this question. The maximum value in both of these languages for integers is

    Number.MAX_VALUE === 1.7976931348623157e+308 Any factorial greater than 170 will give an integer of above 7.257415615307994e+306 which javascript will interpret to infinity causing the 1/n! to go to NaN

    just I know some people are having this problem and maybe theres a fix but I figured I would raise the issue regardless. Python has an upperbound on integers based off device limit so if anyone attempting JS/TS has the same issue but want the challenge of solving the problem still they can try python ✌🏻

  • LunaTMT Avatar

    Please could someone point me in the right direction towards simplifying the expression as I'm lost. I'm not asking for the answer just a little more information.

  • KKQQ Avatar

    going <- function (n) { # your code c <- c() for (i in 1:n){ a <- factorial(i) c <- sum(c,a) } d <- (1/factorial(n))*c return(d) }

    the code works in vscode but fails on the website, why?

  • hobovsky Avatar

    This comment has been hidden.

  • itmeine Avatar

    [Swift] when (n > 170) the output is allways "nan" so kata cannot be complited in this language

  • Yusuf777 Avatar

    I passed the test and all test options,but i'm failing at that it is saying NaN for big numbers(bigger than 180).Could anybody help me how to avoid this,please?

  • firas_attieh Avatar

    My attempt keeps getting timed out, even though it is saying that i passed all of the test cases. I've been optimising my code and ive narrowed the function down to one line of code, still getting the same issue. (python)

  • akar-0 Avatar
  • akar-0 Avatar
  • kayl-xy Avatar

    This comment has been hidden.

  • soltivka Avatar

    it seems like JS doesn't know how to handle such big/small numbers. When the number approaches 200, NaN or Infinity starts to be output :

    { number: 200, result: NaN }, { number: 22796, result: NaN }, { number: 1011, result: NaN }, { number: 10110, result: NaN }, { number: 188, result: NaN }, { number: 523, result: NaN }, { number: 19182, result: NaN }, { number: 10481, result: NaN }, { number: 193, result: NaN }, { number: 180, result: NaN }, { number: 185, result: NaN }, { number: 175, result: NaN }, { number: 46541, result: NaN }, { number: 185, result: NaN }, { number: 45941, result: NaN }

  • Blind4Basics Avatar

    Ruby 3.0 should be enabled (Refer this & this for more detail)

    (see here)

  • Vishnevskiyoleg Avatar

    Cool kata, but after 5 hours I have a problems with stack Exception in thread "main" java.lang.StackOverflowError

    Still thinking how to work with n>6000!

  • Vishnevskiyoleg Avatar

    I spent 3 hours and Execution Timed Out (16000 ms) in Random Tests (((

  • Fahad Hayan Avatar

    My code is giving the expected results but its always faild with exit code: 1. This is my output screen:

    Test Results: going_Tests Fixed__Tests Expected: equal to 1.2125 Actual: 1.2125

  • rayancastro Avatar

    Ruby kata tests numbers higher than 10000

    Javascript kata tests random numbers, no higher than 200

    Should have similar attempt tests between languages

  • Sandhu Avatar

    I want to use Map<number, number> for memoization in typescript but it is not supported in version 2.4. Why can't I use version 3.* to solve this Kata?

  • user1430804 Avatar

    My execution time : 5982 ms Top Solution execution time : 582 ms

    want to kill myself.. ! :(

  • Roman Maru Avatar

    I'm trying to use BigInt object in JS, but getting errror that is not defined. The object is built-in feature. Please advise

  • pyDev69 Avatar

    ISSUE

    This problem has several issues, such as compiling huge numbers in Python 3.8

    I passed 8 test cases with NO remaining cases, yet I was met with an error: 'int too large to convert to float'.

    These problems should not be designed this way.

  • alexcodes Avatar

    Overflow error i tried a diffrent apporoach to the challenge still same problem

  • grabielcruz Avatar

    This comment has been hidden.

  • ejini战神 Avatar
    • Node 12 should be enabled (Refer this and this for more info)

    • Python new test framework should be used (Refer this & this for more detail)

    • Ruby 3.0 should be enabled (Refer this & this for more detail)

    • Typescript 3.3 or above should be enabled, refer this for more info

  • bruschmaker Avatar

    This comment has been hidden.

  • RNOH Avatar

    Nice Kata!

  • Zxkredo Avatar

    This comment has been hidden.

  • Zxkredo Avatar

    "Expected: equal to 1.275 Actual: 1.275"

    • could be a problem with truncating my solution with std::round(result*1'000'000)/1'000'000
    • i need long double for this step
    • i tried casting (double)result
  • Max Kamerov Avatar

    I got :

    Expected: equal to 1.275 Actual: 1.275

    _(--)/

  • user117580 Avatar

    This comment has been hidden.

  • jvquarck Avatar

    There are no primitives for big floating numbers in javascript. If so, why offer that language?

  • themwq Avatar

    This comment has been hidden.

  • cybersinger Avatar

    This comment has been hidden.

  • zsolt-szilagyi Avatar

    This cannot be solved in PHP, as the tests are flawed:

    "Failed asserting that 1.146652 matches expected 1.1466510000000001."

  • zacharyboeder Avatar

    Hello,

    I'm working in R and running into issues regarding large values (specifically going from 170 to 171). Is the remark at the bottom of the description an indication that we will need to install a package to handle large inputs? Or is there a way to work around the 171 problem in base R? Note: I am only wanting a "Yes, there is a way to work around the 171 problem in base R" or "No, there is not a way to work around the 171 problem in base R." I have never tried installing packages in CodeWars and therefore have no idea if it is even a thing or not.

  • gardiner13 Avatar

    I ran my code a couple time - I am passing all the tests, but I am also getting an exit code. Am I doing something wrong? I tried adding import sys sys.setrecursionlimit(10**6) into my code, but then it doesn't run at all on here - runs fine on my computer

  • Alex.I Avatar

    Solve in Ruby, use bigDecimal Fail in 62 Test-case for n ~ 48_000 It needs more than 12 seconds for work.

  • Vlad160 Avatar

    This comment has been hidden.

  • Ashyk Avatar

    if n > 170, then it return NaN, using JS, with others numbers it works

  • drcz Avatar

    Hi, racket version is initialized with (require going), should be (provide going), otherwise the tests don't work.

  • krishp Avatar

    The hardest part was trying to figure out when you should round or limit the number to 6 places.

  • Midgarddd Avatar

    In C langueage I receive statement like this "The expression (expr) - (act) <= (1e-6) && (act) - (expr) <= (1e-6) is false." and I don't understand it. Anyone know waht I do wrong? Almost every my test passed, but sometimes I had diffrent number on last digiit. Is it allowed?

  • JHSN Avatar

    This comment has been hidden.

  • inyoot Avatar

    I already use double long and the test case use 10110 which mean 10110! = 5.229e+36099 and the max capacity of double long is only 1.18973e+4932. Any suggestion?

  • yerlanyr Avatar

    This comment has been hidden.

  • psychsane Avatar

    Getting error - java.lang.NumberFormatException: For input string: "�"

  • neugierige Avatar

    Hi, I'm using Swift and I get a "exit code 132" when I run the attempt. No problem at all with the running the sample tests. There is no information provided on what is causing the issue. Any ideas?

  • eric7237cire Avatar

    This comment has been hidden.

  • allahaka Avatar

    In php
    $res = substr($res,0,strrpos($res,'.')+7);
    is giving NAN, but,
    $res = substr($res,0,strrpos($res,'.')+6);
    is giving correct result without last number.
    When I was testing it on my server it was working without problems.

    PS. Only 7 isn't working

  • tarekbadrshalaan Avatar

    I'm using GO 1.11

    I found an issues in my solution when i run locally

            main_test.go:42: expected 1.005025; got -9.223372036854775e+12 
            main_test.go:42: expected 1.001915; got -9.223372036854775e+12 
            main_test.go:42: expected 1.00099; got -9.223372036854775e+12 
            main_test.go:42: expected 1.000098; got -9.223372036854775e+12
    

    even after got the Best Practices solution the same issues still exist.

  • EmanuelGauler Avatar

    I think there is a mistake with the examples. going(8) => 1.146652, but in the example is 1.146651.

    I use Ruby

  • lciamp Avatar

    1.052787 should equal 1.052786 ??? 1.052787 is the output and I'm not even rounding it.

  • pskirank Avatar

    How to handle these ?

    1.146652 should equal 1.146651 1.052787 should equal 1.052786

  • Phil157 Avatar

    I love it when a plan comes together - in the search for accuracy a whole different formula emerges :o)

  • dotJack Avatar

    This comment has been hidden.

  • vikash_124124 Avatar

    I need some help. Solving it using R. I have resolved the accuracy issue but getting NaN(Not a Number) output for a few cases. Seems like i am missing a few test cases. Can someone please suggest how to overcome this problem?

  • erichlf Avatar

    This comment has been hidden.

  • bartekltg Avatar

    The test uses equal comparations on floating points (this is bad, very bad practise!). We have to round it to 6 digits using floor(). This is still bad, because the results could be in the form 1.24499999999564, while answer is 1.245. The numbers are close enought, but by comparing truncated number we get wrong answer. If no one want to repair this one, just remember to compare floating points in proper way next time:)

  • stan4cb Avatar

    This comment has been hidden.

  • user6341742 Avatar

    Hi, Im having a problem with the solution in C.
    Using a double will get me stuck on test 8 where it will reach Infinity and NaN.
    I got around that by working with long double and casting the end result to a double.

    But that gets me stuck o the test where

    int n = 10110;
    

    as it will result in the following:

    // (1 / n!)   * (1! + ...n!)  = Un
    0.000000e+00   * inf          = -nan
    

    I fixed it with an ugly hack where i return the expected result if the result is NaN.
    I tried some googling to find out what data type i could use, but no succes yet.
    I would love to know how i can solve the kata correctly. I really like this one

  • user1055863 Avatar

    This comment has been hidden.

  • aamir7117 Avatar

    Great Kata. Thank you!

  • bbharat3 Avatar

    This comment has been hidden.

  • mdib Avatar

    This comment has been hidden.

  • venom33 Avatar

    I dont know why but it seems PHP rounds numbers (by function round()) the different way then the tests expects or maybe it depends by OS or CPU registers. But if we do round by "low-level-coding" (without predefined standart functions then it works fine).

  • attilio Avatar

    This comment has been hidden.

  • integral Avatar

    I'm getting the same error as others in C++.

    Expected: equal to 1.2125
    Actual: 1.2125
    

    Any ideas?

  • Toderesa97 Avatar

    This comment has been hidden.

  • victorsilent Avatar

    This comment has been hidden.

  • DrGre Avatar

    I think there is a problem with the C++ testing. I got the following: Fixed__Tests ✘ Expected: equal to 1.17321 Actual: 1.17321

    Random_tests ✘ Expected: equal to 1.05574 Actual: 1.05574

  • erad Avatar

    Hi all,

    Trying to implement a solution using Java. But I can't achieve a requested performance level :( Tried to use BigInteger and BigDecimal to deal with factorials of big numbers but in this case I get a timeout error after 3 or 4 successful tests.

    Could you please drop a hint which the algorithm should I use?

  • voa1953 Avatar

    In description: "Are these number going to 0 because of 1/n! or to infinity due to the sum of factorials?"

    But un = (1 / n!) * (1! + 2! + 3! + ... + n!) are always greater than 1 and never going to 0! Or I do not understand something?

  • TerranoCode Avatar

    This comment has been hidden.

  • Coldberg Avatar

    keep getting these kind of errors

    Fixed__Tests
    ✘ Expected: equal to 1.14665 Actual: 1.14665

    Random_tests
    ****************** Random Tests **** going
    ✘ Expected: equal to 1.04007 Actual: 1.04007

  • saurus Avatar

    There seems to be a problem with C++ tests. My solution fails even though it returns the correct result, eg.

    Fixed__Tests
    ✘ Expected: equal to 1.275 Actual: 1.275

    The problem I believe is in the floating point comparison, if I replace the line

    Assert::That(ans, Equals(sol));
    

    with

    Assert::That(ans, EqualsWithDelta(sol, 0.000001));
    

    then the local tests pass.

  • astepanel Avatar

    Clojure naive solution with memorize falls with StackOverflowError in Clojure 1.6, but work in 1.8.

  • LivDead Avatar

    Please check tests in the C# solution. I always receive "unknown error".

    Compilation succeeded - 1 warning(s)
    {"stdout":"","stderr":"/tmp/csharp116421-20-skhjib/fixture.cs(12,8):
    warning CS0414: The private field `SuiteTests.randomGenerator' is assigned but its value is never used\n","wallTime":0} Unknown error

  • Saeif_Saleh Avatar

    This message keeps emerging. Process was terminated. It took longer than 6000ms to complete

  • r.karpievich Avatar

    Hi. I got an error for n = 10110, "Process was terminated. It took longer than 10000ms to complete". What can I do?

  • riaz Avatar

    Can someone provide the input value for test10 and test11? I'm getting errors in these testcases and cannot figure out why.

  • geresdi Avatar

    I still have the rounding issue with Python. round(result, 6) doesn't help either as the last digit may differ from what is expected. Right now I have to use a rather ugly math.floor(result*1e6)/1e6 statement. Would this be possible to fix? Thanks!

  • matt1024 Avatar

    Hi! I also noticed the rounding issue.

    The task asks for 1.2125, but that is a repeating fraction that can't be represented exactly in binary (like 1/3 is .333 etc in decimal), so once put in a double it is around 1.2124999999999999. When the program correctly truncates that to 1.212499 it fails the test case.

    In general rounding won't work math wise for comparing floats, because it will tolerate a varying amount of error that might be infinitely small. For example if you are truncating .9999999999 to 0, with only .0000000001 error it will truncate differently to 1. But for .5 you can have a whole .5 of error before it will change the truncation.

    Luckily this is really easy to fix, the testing framework already accounts for this and you can just add a third parameter to Assert.AreEqual(expected, actual, tolerance) which is the tolerance amount. If you make this value large enough, say .0000015, it should work with all the existing solutions too.

    Thanks!

  • StoPizzot Avatar

    This comment has been hidden.

  • marmotteNZ Avatar

    May be interesting in terms of code writing to address the large numbers, but mathematically it is just boringly trivial :(

  • silentZaika Avatar

    This comment has been hidden.

  • quangle Avatar

    "Keep in mind that factorials grow rather rapidly, and you may need to handle large inputs."?

  • ya_sh Avatar

    From the perspective of mathematical analysis it's very easy to tell that the series does go to 0, because 1/n! multiplied by n! equals 1, and n! is the divisor of the rest of addends :)

  • NazarSamurai Avatar

    Hi all. Please give me a tip, which algo did you use to div extra large numbers. In my case I used exp^(log(a) - log(b)) algorithm but this algo give me the accuracy to the fifth decimal place, but in some test cases I need accuracy to six decimal. Please help. P.S sorry for my english.

  • vlad_karp Avatar

    This comment has been hidden.

  • JoeStrout Avatar

    All the solutions depend on rejiggering the problem to a different form that avoids huge numbers. Implementing the equation as given doesn't work within the allotted 6000 msec (though it does work, if you use BigInteger, of course).

    So this is a math problem, not a programming problem.

    But I don't understand the math in this case. Can somebody explain it, or point to the article on the web where they found it? (My google-fu is not strong enough.)

  • jjinking Avatar

    My python code works fine when I click on "Run Tests" but when I click "Submit" I get the following:

    Submission timed out while communicating with our servers. This may infrequently happen if our servers are extremly busy. Please try again. If issues persist you can check status.codewars.com to see if server issues are being investigated.
    

    This happens over and over, but if I change the code to give an erroneous result and submit, there aren't any communication errors. I also checked status.codewars.com and everything seems to be running normal.

    I wrote my own memoize and factorial functions, and my truncation function converts Decimal to float.

    Does anybody have any clue what's going on? I'm relatively new to the site, and I've completed only about 20 problems so far, but this is the first time I'm encountering this error.

  • qwertz24 Avatar

    This comment has been hidden.

  • willfish Avatar

    So I've been bashing my head against this for a while. I've got it to work with various factorial algorithms locally (variations on recursive, variations on iterative, stirling, using a matrix constant, combinations of these and others).

    For really large n! my session times out.

    I'm getting to the point where I think this actually doesn't work in Ruby?

  • LeanMC Avatar

    Please help me understand:

    Expected: 1.275, instead got: 1.275000 Expected: 1.2125, instead got: 1.212500 Expected: 1.173214, instead got: 1.173214 Expected: 1.146651, instead got: 1.146652 Expected: 1.052786, instead got: 1.052787 Expected: 1.034525, instead got: 1.034525 Expected: 1.020416, instead got: 1.020417 Expected: 1.008929, instead got: 1.008929

  • s_bamboo Avatar

    hello, i got this "Expected: 1.005025, instead got: NaN" ( when n >200 ) why is that?

  • Thanos983 Avatar

    This comment has been hidden.

  • Pyaria Avatar

    Hi guys, I have a problem trying to solve this for the submission cases in Python.

    For the third test case,after rounding, this is the response I get: 1.146652 should equal 1.146651

    However, when I print out the float prior to rounding, I see that it is 1.14665178571, which, to my understanding, means that the answer should be 1.146652 instead of 1.146651...

    Where am I going wrong??

  • marcelocg Avatar

    This comment has been hidden.

  • glithc Avatar

    This comment has been hidden.

  • TheRealOC Avatar

    would help to clarify the need to handle large inputs

  • user8866973 Avatar

    In Python I get "OverflowError: long int too large to convert to float" when submitting the result function. May be you should keep the first value in test cases lower than 170 for python?

    look at the first answer - http://stackoverflow.com/questions/16174399/overflowerror-long-int-too-large-to-convert-to-float-in-python

  • bkaes Avatar

    Truncating a result usually leads to bad results. Instead, check in your tests whether the user's result is close enough to the actual one (note: I come from computational science, and you would be quartered there if you throw away any significant digit, in case you're wondering why I don't like that).

  • bkaes Avatar

    You just published your solution in your example Haskell test cases. And while I appreciate the effort, you don't need to provide all languages at once. This doesn't give you more honor, but can lead to sub-par tests if you're not proficient in all those languages.

  • Darnor Avatar

    This comment has been hidden.