5 kyu

Diophantine Equation

1,613 of 4,865g964
Description
Loading description...
Fundamentals
Mathematics
Algebra
  • Please sign in or sign up to leave a comment.
  • SidoTech Avatar

    Hi ! I'd love to translate this kata to make it available in Scala 3, as only Scala 2 seems to be available, but I can't seem to figure out how to do that (when I try to add a translation, Scala doesn't show up since the translation already exists - but only for 2.13) Any idea how I could do that ?

  • artem-totality Avatar

    Good mathematical average level kata!

  • ahmet_popaj Avatar

    Very hard and challenging katas.

  • Egor318 Avatar

    Очень интересный ката. Автору списибо большое

  • batyrqh Avatar

    This comment has been hidden.

  • ejini战神 Avatar

    JS Node 18. should be enabled with mocha + chai framework used and logs removed

  • ejini战神 Avatar

    Ruby 3.0 should be enabled

  • ejini战神 Avatar

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

  • saudiGuy Avatar
  • Odin95 Avatar

    could somebody please explain to me why x2 - 4 * y2 = (x - 2y) * (x + 2y)?

  • nomennescio Avatar

    Kata fails for C translation when list of pairs is not in sorted order, which is not an explicit requirement.

  • NonBrewed Avatar

    I am not able to import sympy. My solution involves:

    from sympy.solvers.diophantine import diophantine

    from sympy import symbols

  • Dimitrius1011 Avatar

    I can't solve this problem due to specific test 11. This is so weird that second pair of numbers require blanc space before them.It seems to me, that it shouldnt work like that. expected:<[[4505, 2252],[ [1503, 750], [647, 320], [505, 248], [415, 202], [353, 170], [225, 102], [153, 60], [135, 48], [103, 20], [97, 10], ][95, 2]]> but was:<[[4505, 2252],[[1503, 750],[647, 320],[505, 248],[415, 202],[353, 170],[225, 102],[153, 60],[135, 48],[103, 20],[97, 10],][95, 2]]>

  • seleemdz Avatar

    In C, why the returning parameter is a double pointer ?

  • gezamiklo Avatar

    I am trying to solve this Kata in swift. My code 900000009 gives me 11 valid results and you expect 6 only. Is there any other limits that are not revealed in the instructions part?

    I was trying to find the right limits, but gave up. The limits really affect the time the algorythm takes to finish. Thanks for any tips.

    My results are [(450000005, 225000002), (450000003, 225000001), (450000001, 225000000), (449999999, 224999999), (449999997, 224999998), (449999989, 224999994), (150000003, 75000000), (50000005, 24999998), (26470597, 13235290), (8823555, 4411752), (2941253, 1470550)]

  • Iluxmas Avatar

    This comment has been hidden.

  • akar-0 Avatar
  • quang040403 Avatar

    can you guys help me with this issue pls, before facing this problem, i have to duel with time out, but after changing idea, my result became this "Test Crashed Caught unexpected signal: SIGSEGV (11). Invalid memory access." Does that mean my idea have some problem and I have to fix and can anyone leave me any advise pls? Thank you!

  • IvanRia Avatar

    what is a parameter of the function solEquaStr() int* length?

  • kechja Avatar

    This comment has been hidden.

  • user7828840 Avatar

    This comment has been hidden.

  • yakosvetlana Avatar

    All tests are passed. But it writes * Execution Timed Out (12000 ms) * because of which the test is not submitting. I tried it differently ... but still does not start up because of Timed Out. What to do?

  • hobovsky Avatar

    It's totally not clear what should be returned in Java when there's no solution.

    Additionally, assertion messages are confusing af due to diff markers inserted by JUnit. Simple return ""; results in:

    test12
        expected:<[[[45001, 22500]]]> but was:<[]>
    test13
        expected:<[[]]> but was:<[]>
    

    At least a sample test for input with no solution should be added, but it would be much better if assertion messages (or, ideally, format of the output) were improved.

    See post by ozgurkircak below for some background.

  • ozgurkircak Avatar

    What is the difference between [[]] and []? Both are for no solutions but my soln could not pass the test because of that. expected:<[[]]> but was:<[]> In instructions it is said that for no solutions return "" or [] and that is what my code returned. What is wrong?

  • xsherryhe Avatar

    This comment has been hidden.

  • DrColossus Avatar

    This comment has been hidden.

  • user1362897 Avatar

    This is the third time I get : Failed Execution Timed Out (12000 ms) my code solves the kata. I can´t to submit does anyone know why?

  • Arya_Poddar Avatar

    In R translation, the output is expected as list converted to String. That's a very poor choice of expected output.

  • Tzigitzellas Avatar

    Any suggestions as to why this times out would be appreciated. Works fine in PyCharm.

  • dfhwze Avatar

    Clojure translation taken down?

  • Micael7 Avatar

    Great Kata ! I remember not being able to solve it a year ago, back while coding in C++. Although i usually don't have big troubles with 5 kyu math type katas, here smoehow the math part was a little tricky for me. After a year of break, now in Python, somehow had better luck with this one :D

  • mytarenayalabora Avatar

    Is that possible to solve it on Python? I have always "Execution Timed Out", but all tests is accepted

  • Unnamed Avatar

    This comment has been hidden.

  • Khanthavone Avatar

    This comment has been hidden.

  • Bubbler Avatar

    Factor translation submitted.

  • monadius Avatar

    testSolequa should be changed to solequa in the initial F# solution.

  • The_Paradox Avatar

    nice challenging kata

  • ehsan.gdrzi Avatar

    Using javascript, one of the tests tries 900000009 which is bigger than js safe integer limit and we need to use BigInt instead. But the interpreter doesn't support BigInt! So I think this is an issue with this problem.

  • user3688406 Avatar

    Is it possible to solve in python? I am using only one loop, no additional packages. I limited the iteration interval, limited even and odd numbers for the different cases and even then it doesn't pass the time test.

  • cdaqua Avatar

    This comment has been hidden.

  • mwk48 Avatar

    This comment has been hidden.

  • rge123 Avatar

    I think the random tests in python are calling my function twice (when I run a print(n) it logs it twice). Why?

  • hobovsky Avatar

    In C++, kata produces unhelpful and very confusing assertion messages (see Lezh1k's post below), because it lacks stringizer for returned type. See this discourse for details how to fix it.

  • Lezh1k Avatar

    Expected: equal to [ [unsupported type] ] Actual: [ [unsupported type], [unsupported type], [unsupported type], [unsupported type] ]

    Have no idea what does it mean and what went wrong %) Any hints?

  • EdwardALockhart Avatar

    This brought me back to my high school days with a nice page of math workings and some revision. Very satisfying when it was finished, thanks!

  • shaardie Avatar

    Seems broken for Haskell. Even a dummy function like

    solequa :: Integer -> [(Integer, Integer)]
    solequa _ = [(1,2)]
    

    returns this error:

    Codewars/Kata/DiophSpec.hs:1:53:
        Not in scope: `main'
        Perhaps you meant `min' (imported from Prelude)
    
  • HereChen Avatar

    This comment has been hidden.

  • tokyo_s Avatar

    This comment has been hidden.

  • Andrey1960 Avatar

    from sympy import symbols

    from sympy.solvers.diophantine import diop_DN

    I solve this kata using module sympy

    I import from the module sympy, and the test program swears and prescribed import sympy does not help

    ModuleNotFoundError: No module named 'sympy'

    Give some advice please

  • Haggr Avatar

    It needs to enable Node 10 for JS because you cannot use BigInt otherwise which makes it unnecessarily difficult. I can't calculate x * x because it overflows. I am still thinking on how to calculate y at O(n) without x^2.

  • Aphirat Tta Avatar

    Hey i need to know what number for basic case test i got this error, then i try find number, but [450000005, 225000002] and [2941253, 1470550] not same result of x^2 - 4y^2 = n

    Expected <[][]int | len:15, cap:16>: [ [450000005, 225000002], [450000003, 225000001], [450000001, 225000000], [449999999, 224999999], [449999997, 224999998], [449999995, 224999997], [449999993, 224999996], [449999991, 224999995], [449999989, 224999994], [225000004, 112500001], [150000003, 75000000], [50000005, 24999998], [26470597, 13235290], [8823555, 4411752], [2941253, 1470550], ] to equal <[][]int | len:6, cap:6>: [ [450000005, 225000002], [150000003, 75000000], [50000005, 24999998], [26470597, 13235290], [8823555, 4411752], [2941253, 1470550], ]

  • IvanBayan Avatar

    Why order of answers is important?

    Error. Expected {{4503, 2251}{903, 449}} but got {{903, 449}{4503, 2251}}

  • user6277683 Avatar

    Hi; hobbyist programmer here (non-STEM background), wondering if anyone could point me in the right mathematical direction so I can fully solve this... Not looking for the answer itself!

    Using some previous discussion posts, my solution now calculates the right answers with only one loop (x = n; n >= sqrt(n); x--) & (derive y from current x value), it's reaching numbers in the billions, but still timing out (running ~20 seconds on my local machine to solve the number I'm timing out on!!).

    I've read about Diophantine equations, but my math is weak; so I'm still missing something - anyone have a non-spoiler / mathematical suggestion for me?

  • YiJian006 Avatar

    This comment has been hidden.

  • eb110 Avatar

    Hint - don't look for O(n) solution - it is kind of "brute force" kata.

  • S4K4L04 Avatar

    i am new to programing picked it up a few weeks ago so i made a code that works but its very slow it has 2 for loops and takes 30 sec to compute n = 10k

  • Cezar Avatar

    Code should return empty solution, in case n is not positive. It is not straightforward from the description.

  • devLeopar Avatar

    This comment has been hidden.

  • krishp Avatar

    Can someone explain how you would solve this? I am looking through other solutions and I really cannot understand how they were able to do it.

  • hardkoded Avatar

    This comment has been hidden.

  • DarkestHobbit Avatar

    Thank you sir for this great kata :D

  • avinashsinghdhillon Avatar

    This comment has been hidden.

  • sabolc Avatar

    Something is weird with the Javascript tests. My code should be OK (the same algorithm passed in Python with no trouble whatsoever), but 4 of the tests give a message like this:

    ========== Expected: '96', instead got: '[[4505, 2252], [1503, 750], [647, 320], [505, 248], [415, 202], [353, 170], [225, 102], [153, 60], [135, 48], [103, 20], [97, 10], [95, 2]]' ==========

    Out of curiosity, I added this to the code: if (n == 9009) return 96;

    and then it turns out that the tests expect the value I pass normally:

    ========== Expected: '[[4505, 2252], [1503, 750], [647, 320], [505, 248], [415, 202], [353, 170], [225, 102], [153, 60], [135, 48], [103, 20], [97, 10], [95, 2]]', instead got: '96' ==========

    Can somebody help explaining what is going on? I do not have very much experience with Javascript yet.

  • randallgyebi Avatar

    This comment has been hidden.

  • seger Avatar

    0% programming, 100% math. Formatting the answer string is pointless and annoying.

  • mollyculeofwater Avatar

    Finally getting to the answer was so satisfying (:

  • cyan4936 Avatar

    Hey guys, I am passing all the sample test cases but am constantly timing out.

    For me, my algorithm runs x from square root of n up till n, and for each iteration of x, runs another y from 0 up till half of x.

    i.e. Sqrt(n) <= x <= n and 0 <= y < (1/2)x

    I got the above equalities by playing with the conditions provided. What have I missed out that is making my loop so inefficient?

  • nnivx Avatar

    This comment has been hidden.

  • wolfenstein11x Avatar

    This comment has been hidden.

  • danalaxy Avatar

    Great Kata, thank you! Maths is gorgeous!

  • frefer Avatar

    really nice kata, what a challange !! thank you

  • MP7373 Avatar

    I think this kata deserves to be a 4 kyu or a 3 kyu problem because it took me much longer to get a fast solution than a 5 kyu problem normally takes. I've seen other people comment similarly which is what prompted me to say this.

    Overall a very satisfying problem to solve I just think its difficulty level needs to be changed.

  • Voile Avatar

    No random tests.

  • Nehal Avatar

    Results are matching for run sample. But when click on attemp following error comes.

    Process was terminated. It took longer than 12000ms to complete.

    Same thing happening in few other katta solution submittion.

  • ango08 Avatar

    This comment has been hidden.

  • rvolovik Avatar

    I have some troubles with the applying my solution. As I increment from 0 to ... I have a mismatch with the required result.

    [[903, 449], [4503, 2251]] should equal [[4503, 2251], [903, 449]]

    The same thing with this [[95, 2], [97, 10], [103, 20], [135, 48], [153, 60], [225, 102], [353, 170], [415, 202], [505, 248], [647, 320], [1503, 750], [4505, 2252]] should equal [[4505, 2252], [1503, 750], [647, 320], [505, 248], [415, 202], [353, 170], [225, 102], [153, 60], [135, 48], [103, 20], [97, 10], [95, 2]]

    Is the proposed answer correct or not? All other tests before specified are passed

  • cowile Avatar

    Using C, I question why the return type is Pair**. I would expect it to be Pair* so I can allocate the array with Pair *dio = malloc(10 * sizeof(Pair)), resize as needed, and return dio.

    That is easier than needing a new malloc call for every pair.

  • pyramidka Avatar

    This comment has been hidden.

  • kronosjt Avatar

    If I simply increase the range of my search by 1 it times out. I think it would be good if at least the passing tests were printed out before it times out. That way we know which test timed out and can optimize for those tests. I've also noticed that simply adding an else block for a case where [] is the answer causes a time out!

  • JanWarlen Avatar

    expected:<[[4505, 2252],[ [1503, 750], [647, 320], [505, 248], [415, 202], [353, 170], [225, 102], [153, 60], [135, 48], [103, 20], [97, 10], ][95, 2]]> but was:<[[4505, 2252],[[1503, 750],[647, 320],[505, 248],[415, 202],[353, 170],[225, 102],[153, 60],[135, 48],[103, 20],[97, 10],][95, 2]]> eh...i think this info is not "correct"

  • metalbot Avatar

    Thanks for this. I like Kata for which brute force times out and makes me think about the math. Plus, it's one of the few that I could solve with a (convoluted) one liner.

  • geeves Avatar

    I get to test 23 using PHP and it times out. Sigh. I'll keep working on it. Did others struggle with processing time?

  • j3harvey Avatar

    I am completing this problem in C++, and whenever I sumbit I get no errors, 0 tests passed, 0 tests failed, and no way to proceed.

    Is there a problem with the C++ test suite for thsi problem, or has anyone experienced this problem before?

  • chinelli202 Avatar

    I would suggest the precision be made that n should be strictly positive (n > 0) and not simply positive (n >= 0). because the eventuality of n = 0 yields an infinity of (x,y) solutions statisfying the equation (every couple (0,x) where x in a multiple of 4 for instance)

  • Garcian1991 Avatar

    Well, that was tough. I spent all day to make my solution. Thank's a lot, nice kata!

  • MBogda Avatar

    This comment has been hidden.

  • le0 Avatar

    This comment has been hidden.

  • chaoshitter Avatar

    Hint and mathematics is very important!

  • nozturk Avatar

    This comment has been hidden.

  • Stefannel Avatar

    This comment has been hidden.

  • dsmoker Avatar

    I can't help but feel this deserves to be in a harder kyu; the programming itself is trivial but the algorithm relies on doing a bit of maths. Excellent kata...

  • pbweb Avatar

    I don't know what the problem=( Test 26 - I got 15 pairs of numbers. I can't figure what result must be=(

  • _Alexey_ Avatar

    Please fix problem with int32 overflow - are these numbers suitable or not? different test cases require different numbers

  • alessandrodalbello Avatar

    Best kata I've faced so far!! I've spent many hours to find an algorithm that could run in reasonable time, and then, finally, I've focus my mind on the hint. Very nice exercise, congratulations!

  • BobBash Avatar

    This comment has been hidden.

  • shankararunachalam Avatar

    This comment has been hidden.

  • Jenik Avatar

    This comment has been hidden.

  • ruprekht Avatar

    This comment has been hidden.

  • krzkaczor Avatar

    Shouldn't it be somewhere noted that A that is mentioned in description is actually parameter n (at least in JS code it is named n)

  • KevOrr Avatar

    This comment has been hidden.

  • vladkha Avatar

    I'm sure, that there is a problem with the test case "Test11" in Java: there are some extra brackets and no comas between solutions, but it is required in task description

  • supersevket Avatar

    Sorry to say, but I think, in Java tests one of them is wrong. My code works fine except that one, and to check I calculated with hand, an mine is still correct. You need to check that. Test number is 28.

  • iammrinal0 Avatar

    am getting a timeout error while submitting. can someone help me out? for the same test case it works if I add it to my test cases. what am I missing?

  • user1580646 Avatar

    0 is not positive integers.

  • Bodigrim Avatar

    This kata will be much more challenging, if test cases included really big integers.