1 kyu
Express number as sum of four squares
71 of 114dolamroth
Loading description...
Performance
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.
This comment has been hidden.
Please, do not use such approaches, this is considered cheating on Codewars.
I see that's definitely the case for this kata, that's why as soon as I noticed I made this comment.
Some katas are literally about finding ways to break expectations of the language, I had no way of knowing this wasn't one of them, instead had false clues pointing to the fact that it might be.
I would have retracted my solution if I knew how. Now I've tried to submit another one, which is not just a plain copy of someone else's solution, although most solutions are just a copy of someone else's code, some verbatim (but that's not considered cheating?), but after I submit I'm still told solutions have been withheld, and I lost the score/honor associated with the kata :/
It's not the end of the world, but it feels like this wasn't my fault and I wasn't given a way to correct it, just penalized, after I spent over 24hrs working on this challenge with more good faith than the other solutions I've seen.
(preemptive disclaimer: I cannot see the two top comments because they are hidden as spoilers)
Ad "Some katas are literally about finding ways to break expectations of the language, I had no way of knowing this wasn't one of them, instead had false clues pointing to the fact that it might be." : kata which are literally about breaking expectations of the language are not tagged as
mathematics
andperformance
. I am not sure what could be the clues pointing in this direction, but whatever they are, they should be removed or made look less clue-like.I admit that the line between "breaking expectations of the language" and "unacceptable cheating" can be sometimes blurry, but usually is drawn at the point where solution attempts to directly affect tests: override functions of the testing framework, fool assertions, prevent or impact execution of tests. I am not exactly sure how "find four numbers" would call for such means, but maybe I am missing something :)
The honor/score associated with the kata has been lost because this is how system works when a solution is flagged as a cheat solution. When a moderator marks a solution as a "universal cheat", the solution is hidden (to not spread), and the reward is withdrawn with a small penalty.
Ad "most solutions are just a copy of someone else's code, some verbatim (but that's not considered cheating?)" : plagiarism is considered unfair, and is also handled. Copied solutions are usually handled in a similar way as universal cheats, except of hiding: users who submitted them have their reward retracted. Regular users have no way of knowing if a solution they see has been penalized or not, but I in case of this specific kata, I would be surprised if they were not. You can also report dishonest users on
#contact-mods
channel of Codewars Discord.Additionally, keep in mind that when stuck, you can always ask for help in the
#help-solve
channel of Codewars Discord. Users there are always willing to help, and I am pretty sure that many users who solved this kata struggled with it for much, much longer than a day :)You could have asked a question on the dashboard regarding performance requirements and legit strategies to solving this problem. Also, those other kata's that DO require us to exploit language features are 6-5 kyu. Did you really think a 1 kyu kata has an expected solution with a simple one line exploit as you pulled off?
I see thanks, I had no idea that discord existed!
That was 24hrs of 4-8hrs over several days while I have a full-time job. I would have happily continued with this kata without workarounds if I knew, and if the tests accounted for some of these kinds of cheats (which many katas do) that would have been an easy way to realize it was indeed a cheat and not an intended solution. The moment I saw that the other answers weren't cheats I started writing the comment, which if you can't see, is also marked a "suggestion" to add a one-line test that I would be surprised would be left out if not intentionally. But it happens. The clues I refer to are some of the other comments in the discourse, that talk about "rewriting from scratch" and using different approaches. I originally considered those to mean using a different mathematical approach, but over time I started wondering. Would have been fixed by a one-line test.
And yes I was slightly surprised that a 1 kyu kata might be solved with a workaround, but maybe that was exactly the point of why it was hard. Knowing that you can do the workaround is not to be expected, and getting there requires some relatively creative thinking. The test that would invalidate it would simply be helpful for people approaching the task normally. I haven't solved a lot of 1kyu katas, but for other kyus it's pretty common to have that kind of test or something that would invalidate the workaround. Maybe figuring out that wasn't being tested for was the challenge, I had no way of knowing. Also note this kata is marked as the easiest 1kyu. And for those that can't see the original comment: the workaround is not messing with any test functions or codewars/test internals, it's standalone javascript: I was returning an array of 4 elements that when squared and summed led to the answer, it was just enough in the spirit of the challenge that I figured that might be it. A similar argument can be made that a 1kyu kata might need to be better tested than higher kyu katas, but I don't know if that's really the case.
I admit I did not see the tags at all, when you pointed those out I did feel pretty dumb. For sure in the future I'll double check on the discord or post a comment on the discourse before submitting an answer that I'm not 100% sure is in the spirit of the challenge. I already felt like I was cheating a little bit by looking at guidance on the discourse, let alone get help on a discord, but good to know that's better than experimenting yourself to see if you can solve the challenge, given your solution might not be in the spirit of it. I don't think I should have been penalized, or at least I should now have been able to submit another solution and get my score back, and my request for a one-line test shouldn't have been dismissed, and the fact that I immediately pointed it out in the discourse with a lengthy and respectful comment should have been rewarded rather than penalized.
I'm clearly still a bit upset over losing several days of free time in the context of codewars score, but that's life, I'll move on. I'll look for the discord now, see you there :)
(for the record, this discussion had a follow up on Discord)
My (rather simple) algorithm can do 2^128 numbers in basically no time, except for some special numbers. But above 2^200 it starts to struggle... Any hints? :-/
Try changing your approach. This greedy algorithm takes ~2^65 steps for "worst-case" 128-bit numbers.
I'll ask some AI, I guess
[edit] looks like I already did...
Been sitting for a while now. Tried everything I knew, even lagranges and legendres theorems (iterative reduction too), which work perfectly fine, passes all cases but takes a lot of time. Im failing with the timeout. Tip for others: Do not try to bruteforce and avoid deep recursion, takes a lot of resource and time so not efficient.
This comment has been hidden.
I'm glad, you liked it!
great, but I have to add a spoiler tag for hinting at ways to solving a purple kata.
This comment has been hidden.
bro timeout always no fails but timeout
Try writing a faster solution https://docs.codewars.com/training/troubleshooting#pass-but-timeout
At the beginning I tought this kata was about implementing arithmetic operations for big numbers. Then I saw that some cases had many solutions (which had more than 4 items) and I supposed that it's about some kind of bruteforcing the solutions... And it works for tests on "small" inputs. But I can't find a quick enough solution. So it seems that I should know something more about math to solve the kata. Also I didn't see any examples or simple explanations of the possible algorythm. Maybe once I'll come back with the secret knowledge.
I guess integer factorization is too slow to help with this on the huge numbers...
i've tried my knowledge of checking 2/3/4 squares sum but still failed with realy big random tests.. any tips?
This comment has been hidden.
My program works with all numbers, but does not converge in time, I don't know how else to improve the program. Does anyone have any ideas...
Yes, you need a complete rewrite of the program. Your current solution solves a 6 kyu problem, not a 1 kyu problem. I suggest you read articles about this problem online or in your local library.
My cheap trick did do better than my hard work, it was 28 now i cannot even pass the initial barrier!!
i know its kyu 1 and nobody would help, i dont know if an experienced programmer'll consider wierd, but i did what i could , but some 20-21 digits later it doesn't work, i thought is either it should work entirely or not work at all (could be slow, thats a different issue), but how can it lose its precision !!! i feel i am missing some basic!! if someone generous, give me some advice or reffer to some other kata that may help, i'll be pleased.
This is a well known problem in Math, I suggest you read literature about it. It's a math heavy task.
This comment has been hidden.
When trying to calculate the square root of a very large number I keep getting values a hundred or so too large. I've tried int(math.sqrt(num)) and int(num**\0.5). Is there another way to do this in python?
math.isqrt
Thanks!
This comment has been hidden.
This comment has been hidden.
You are on the right track with that algorithm.
Thank you for pointing me in the right direction
Thank you @dolamroth for this amazing kata, I have been unable to solve anything else on Codewars the past few days as I wanted this to be my 1111th solved kata - now I am finally free (until 2222 at least)!
In addition, thank you for the "honeypot" for attracting interesting Codewars number theory users to conveniently follow; some of their solutions are incredible learning materials also.
Thanks again for your effort in bringing this to CW :+1:
edit- just noticed that I am 36th =
3**2 + 3**2 + 3**2 + 3**2
solver, the numerology is too powerful O_oYou're welcome :)
next kata waiting for you now: https://www.codewars.com/kata/63348506df3ef80052edf587 ;-)
ahaha, i managed to pass big random test, but really big - fails me)))))))))) am i close?))))))))))))))))))))
It seems that to get the point Lagrange is not the only guy i need to get acquainted with.... Legendre is also knocking in my mind... Even if i fail, this is the most exiting challange since i tryed to get merried... My best wishes to author...
This comment has been hidden.
Fixed mistakes with degree (1\2) on huge numbers by couple of "-1 while number**2>n" (uncorrect count), but no way I can pass really big numbers by time limit. May I have some recommendations?
Not to spoil things, I'll just say that brute-force algorithm (which has complexity O(n^0.25), I guess) won't pass here.
Aside from that, I would advise to use
math.isqrt
instead ofint(n**0.5)
, its faster and works with integers of any size, whereasn**0.5
may overflow.I have the same problem(((
What is the difference between using math.isqrt(n) and math.floor(math.sqrt(n))? With the former I could pass the really big tests. With the latter I could not, but to me they do the same thing right?
Python's int type is bigint internally. When you use isqrt, you don't leave bigint domain. When you use sqrt, which is defined to be used in floats, you are limited with input, which fits into double precision limit, 64 bits. The task features integers up to 1024 bits.
Hi,
Nice kata, but IMO the current tests are not really debugging friendly.
If the user's answer has the wrong type, the error message is simply
Expected False to be True
.having four assertions for one test case is confusing, so I suggest combining these assertions into one, like the following.
add small random tests to help users test the correctness of their programs, as the current random tests are too big to debug.
make sure that static tests are consistent across sample tests and full tests.
Thanks for feedback. I will address these issues a bit later today.
So, I've changed the message to more friendly one. (in Python)
As for small random tests, I think the 128 bits tests as they are are fine to debug (at least with PC monitor). Tests with 1024 bits will certainly fail, if 128 bits tests fail, so user doesn't really need to debug them.
As for static tests, I'm not sure that they necessarily have to be equal between sample and main tests.
About static tests, I don't mean that they have to be equal between sample and main tests, but that those appear in sample tests should be included in main tests, and preferably in the same order.
Updated reference Java solution, which used to timeout on certain inputs.
The random tests are pretty weak. Because all the marbles are inside a single 2048-bit test the total runtime totally depends on what kind of number it is, and so the performance test is very luck-based.
I think you should modify it like this:
This comment has been hidden.
Hmmm, now I wonder, why your solution gets invalidated... :) I shall take a closer look on it.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
@Bubbler, @Voile, would you approve the kata, please?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
(a, b, c, d)
or their permutation, such thata^2 + b^2 + c^2 + d^2 == n
, counts.(a, 0, 0, 0)
ifn == a^2
Good. Let's see an efficient algorithm for such huge values.
Which are huge numbers for you? :)
Updated a description
After looking the huge values for tests I think that not only the "teacher wants blood". :)
Performance tag should be added, and perhaps also the bounds of
n
stated in the description.Done