7 kyu
Consecutive Digits To Form Sum
411 of 2,909MrZizoScream
Loading description...
Fundamentals
Mathematics
Puzzles
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.
IMHO current constraints makes it 6kyu. At 7kyu brute-force solutions should be accepted.
This comment has been hidden.
This comment has been hidden.
PHP Translation
in C++, headers
are not required
Damn, this one not that easy. Ps. Once you understand where's the hook not that terrible. But tricky as hell. xD
Lua translation
Update to Scala 3
.
A correct pattern is required, otherwise, everything will be bad)
yes
Nice solution, man)
Given up and unlocked...
This comment has been hidden.
This one is actually pretty easy, the problem is the algorithm timing out.
This comment has been hidden.
COBOL translation (author gone).
approved
Deserves a 5th kyu at least...
Hello hello. First post here. Hoping that someone will be willing to help? I am having the toughest time solving the "Consecutive Ducks" challenge...certainly seems higher than a 7 Kata. = ( From trial and error and reseach, it does seem that I will have to have a static number which I will add too intil the sum value passes that of the arg but that's about as far as I've gotten. Any who, I would appreciate any guidance / suggestion on the direction I should be headed. Thank you. **Javascript
there is a pattern to be noticed about the numbers ;-)
Its going to depend on your math skill. It frustrated me too and that motivates me to learn more math oin my own.
I don't know the math involved, but good thing I know how to look at tests :)
So difficult :((
This comment has been hidden.
I hate puzzles here you have to think outside the box. it's not math. =)
Prolog translation kumited, please review it!
Revised and BIG APPROVAL :+1:
It took me a very long time. Despite the fact that my program is running in Pycharm. Here I see: "SyntaxError: invalid syntax". Has anyone had this problem?
check your python version and make sure you're not doing somehow changing the code while copy pasting
This comment has been hidden.
Usually indicative of simply not running the failing tests locally, not running the failing tests will obviously pass all tests. It might not be, but when you phrase it this way that's the most likely reason from other people's perspective. If you have a particular test case and you share the code you're using (with your post marked as spoiler!) then you'd be allowing others to observe the mystery for themselves. If the expression you show is the whole solution then that is definitely not sufficient. Take for example 10 which can be written as 1+2+3+4, your expression becomes
10 is the value in the kata description
You are right regarding a spoiler, I admit my incaution.
I've marked your OP as having spoiler content, you could have done it too after posting it.
Rust translation kumited! :-)
Revised and BIG APPROVAL :+1:
Execution Timed Out (12000 ms) every time i return bool, True or False. My algorythm works... but i can't return bool.
Not a kata issue, that's a problem with your code.
This comment has been hidden.
This comment has been hidden.
it's an issue with your code, not with the kata; please use the 'question' tag to ask for help ;-)
the problem is that your solution is correct in the mathematical sense, but not in the computer science sense.
log()
returns a floating point number (adouble
) even is that number has no decimal part. that is to say,10.0
is not the same10
. your function always return false because the return type of your expression is alwaysdouble
, neverint
Execution Timed Out (12000 ms)
In order to solve this kata one cannot use brute force logic. Meaning you cannot just try all possible sums of consecutive integers and check if they are equal to N. It's much better to step back and find a general pattern for all N that are true. What do all N that are true have in common?
(Technically in the real world brute force is a (very poor) solution that (kinda) works, but it timing out here in this kata is a good thing. It makes you find a more elegant and faster solution.)
See my solution with added comments if you cannot figure it out. I am aware that there are more elegant solutions out there, but I think my solution will explain why it works in an easy to understand way. Please comment if you disagree and have a better way to explain it. (I love being wrong and to learn new things).
Could someone tell me why the result of the test for 6 is True? because 2+3!=6 and 1+2!6 and 1+2+3!=6
1+2+3!=6
??? Check your math.emmmmmmm...my bad.
Scala translation
Revised and APPROVED :+1:
This comment has been hidden.
This comment has been hidden.
You've written an amazingly inefficient brute-force version of finding the answer, with the search space being literally all possible sums of numbers less than (n-1)^2. For very large n this will take a long time.
The fast, short solution is basically math nerd knowledge and not coding, but even so you could make a better brute force solution that stops checking in the innermost loop once result is bigger than n. You could also think about starting from n and trying to divide it into consecutive numbers rather than simply checking 0, 0+1, 0+1+2 etc. each time. If n can be divided into a sum of k consecutive numbers, then the middle number of the sequence will be about n/k.
Also, line 3 is the guarantee, you don't need to verify it.
Why 69 "False should equal True" if it false? ...10 + 11 = 66 ...11 + 12 = 78
it's any consequtives numbers... I guess you have assumed the first number always 1. e.g, 69 = 34+35
Thank you
Difficulty is very subjective, but I suggest that this kata becomes 6 kyu. Is it technically possible to change the difficulty rating of a released kata?
No, only admin can change it, but there should be very good reason for it.
Thanks for the info. Then it is probably fine to let this be 7 kyu. Only good reason that I would have to change it to 6 kyu is that it could be frustrating for beginners that are legit bad at math, that try their first 7 kyu kata.
This kata is way too difficult to be ranked at 7kyu in my opinion. This is pretty much a discrete math problem, not a coding one
Strong agree. The problem it is equivalent to is 7 kyu, but unless you know that (or realise it quickly)...
This kata is so mean!
sadly description of kata dosent help at all to solve this. and after seeing solutions it lead me to conclustion im too dumb for learning programing
I spent 10 hours+ figuring out a mathematical solution based on triangular numbers and the hint that a solution existed without iteration. Keep trying. I'm probably dumber ;-)
i dont remember when i solved something myself.. so i dont think so
This is really a maths problem (and not an easy one), not a coding one. It says nothing about your abilities as a programmer.
pure math problem. don t code anything !
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I think this kata should contain a hint in the description, about when a number can be expressed as the sum of consecutive numbers, to be ranked as 7kyu.
This comment has been hidden.
Yes. It's equivalent to
while ( true ) { ... }
ordo { ... } while ( true );
, but without block level scoping. When those loops are compiled into machine code, they're reduced to an unconditional jump instruction, i.e.goto
.Why are you trying to avoid a nested loop anyway?
FYI, when you post code to discourse, bracket it with block code formatting so it's easier to read.
I am trying to avoid a nested loop, because it keeps timing out, with what I currently have... And one of comments here, are of someone mentioning their code has two loops, and I think the creator of this kata, mentioning that nested loops will cause the code to time out, basically.
Understood. There are other approaches that have linear and even constant run times. Read up on triangular numbers, for example.
Maybe should have
Mathematics
tag for this one.@dinglemouse
Java translation. Please review.
@clcraig
.
This comment has been hidden.
See the examples in the description, some numbers are not the sum of only 2 consecutive numbers, like 42, you need to add 4 consecutive numbers to get 42.
thanks a lot for answering, I'll try again now!
.
This comment has been hidden.
CFML Translation!
Revised and BIG APPROVAL :+1:
Zizou! How you keeping mate???
Learned something new today ^_^
@fibonaccios
I don't know how to change my algorithm, My code consisted of tow loops, somebody give me please little tip:) I check my code, it works with tests
@alex_prog
This comment has been hidden.
This comment has been hidden.
(manual notification, see above)
This comment has been hidden.
You mean two or more consecutive positive numbers, since otherwise every number has a trivial solution.
Fixed
Since it can't be true in JS, it should be changed ;-)
Fixed. That was actually a description issue.
But actually, the ranges you used in C++ were
2 <= N <= (2 ^ 32)
, so it looks like that was a mistake in the description... JS just has aNumber
, and it's max value is(2 ^ 53) - 1
.2 <= N <= (2 ^ 32)-1
, and Since the Zero Counts as a Positive number , then the Upper Bound of the range should be(2 ^ 32)-1
as originally writtenWhen I checked before I made the edit, it said
(2 ^ 64)-1
... I just edited it to be consistent with what all the languages did...Werid , anyWays , Fixed :+1:
Since you add 2 to the number, the highest number possibly generated is
4294967296
, however, so now the description is inconsistent again... :/To be even more clear, you can specify that the consecutive numbers must all be positive.
done
JS translation. I'll do more when you get more feedback...
To clean up the description a bit, I'd recommend moving the explanation as a comment in each examples' line. Also, maybe put them all in the same code block...
1000000 is too many random tests IMO. In some languages it will display that many lines and crash people's browsers. (Normally you use about 100.)
@Steffan153
That will still crash browsers in some languages - even 10,000 is enough to crash. Maybe we leave it a big number in languages that don't crash your browser when you do that, and I'll use 1000 for others?
Still, a million random tests seems a bit funny... Yes, 50,000 is okay for C++. If you're trying to test for performance, just use big numbers - and you're already doing that, so you're ok.
<random>
can takeCtdlib , and Ctime
, So no need to redundancy :+1:http://www.cplusplus.com/reference/random/ ...
<random>
is about the new C++ way of making integers. srand, rand, and time are C's old way, and are not included in<random>
...This comment has been hidden.
@Steffan153