1 kyu
Express number as sum of four squares
66 of 108dolamroth
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.
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