5 kyu
Weird prime generator
31 of 728g964
Loading description...
Fundamentals
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.
Description issue: description is contradictory w.r.t to
p
it has:as you can see, the third sequence has repeated primes. However the description also says:
from the Discussion I noticed multiple people ran into this and the author replied that we should interpret
p
as the first n distinct primes.In that case, remove the sequence
p: 5, 3, 11, 3, 23, 3...
from the description.Very interesting Kata!!! But description hmmmm... =) Thanks to author!!!
weird it is!!
Description should be language-agnostic
Description should mention the 6th function requires user to return an array not up to the nth term, but containing
n
elements that satisifed the given condition (g(i) != 1
)Ruby 3.0 should be enabled
Enabled in this fork
count_ones
&&over_average
function should also be tested in random tests of all languagesSwift seems to have insane performance criteria. Could you check? I think you are the only solver in this language. [edit] after refreshing, it appears 14 users solved it, not just you. This might change things :)
The needed time is below 6s.
p(n) is tested in Coffeescript.
It seems that the reference solution for max Pn uses the users p(n) method ...
Right! Hopefully fixed.
fixed
The second sequence g(i) is defined as g(i) = a(i) - a(i-1) for i >= 2 with g(1) = 1 !
I´m in trouble on generating the numbers because i don't know what this mean gcd(n, a(n-1)), can anyone give me some tips on this
gcd(a,b) is the greatest common divisor of a and b. It is the greatest number that can divide a and b. example gcd(10,50) = 10 and gcd(5,7) = 1
This comment has been hidden.
Fixed.
question about anOverAverage(n)
In the samples the test calls anOverAverage(5) and expects 3 as an answer.
When looking at the primes in the order the first 5 generated are 5, 3, 11, 3, 23
if I'm interpreting the description correct the values they give is 5/1, 3/2, 11/3, 3/4 and 23/5.
The average of these is about 3.1
Is this how I'm supposed to calculate anOverAverage(n) ?
It was not. I was supposed to calculate the average of
15/4, 18/5, 33/10, 36/11 and 69/22.
It should be 15/5,18/6,33/11,36/12and 69/13.The index of sequence anOverAverage(n) start with 1. But the Araay in code start with 0(in most programming languages)
Doing the C version
Why is maxPn(1) supposed to return 5 ?
If I use 1 to generate the first 1 numbers in the sequence, I will only get 7 (in the actually generated sequence). It'll lead to g(n) only having a single 1 as difference, as we were to start with a leading 1. p(1) for gn(1) and an(1) should return nothing according to my interpretation of the gn() sequence, because it will contain no primes.
Does it in p(1)'s case mean we should generate the sequence until the 1st prime appear?
The problem description of this kata is just very poorly worded, and I don't mean generating the sequence.
In the python translation, function names in description don't match with those expected in code. For example: in code:
max_pn
, in description:maxp
.By the way, It's a great idea to prompt people to split the problem into several subproblems/functions (just like this kata does). Good work g964!
Name corrected. Thanks.
The description is too vague about p(n).
p(n) returns an array filled with the n first distinct primes in the same order they are found in the sequence gn.
Modified.
Please use new python test framework.
Done.
Hi, After solving this, I believe there are a couple of typos that should be corrected in the description:
Shouldn't this be the number of 1s in
gn(n)
?g(n)
is a single number, whilstgn(n)
is the sequence.Similarly should this be
pn(n)
which you refer to in point 5, but never define?Hey @g964, I really love your work here. I've been following your problem since monthes and every time I find solving them fun. But the description of This kata is poorly written. It is hard to understand the problem. Could you please explain the working of function p(n) Again thanks for your amazing katas... Love from India
please elaborate on specific part which you don;t understand
how p(n) fucntion works?
remove all the
1
from sequenceg
and you will getp
sequencethere;s example in description
As mentioned by other people who completed this kata, its description is cryptic, making the whole exercise a trudge to go through. An overhaul of the description would be appreciated.
The description is written incredibly poorly. I spent most of my time trying to decipher it. "a(i)" looks too much like a mathematical function. It was a puzzle to figure out if "a(1) = 7" is talking about outcome of the function or something else. Of course, it was talking about the first member of "a", and that being a sequence. This information is not given in the description, we don't know what "a" is, and have to make up the proper instructions for ourselves. Also this is not a fundemental at all.
The random tests only test for the MaxPn function, as such, the first and the third functions can be cheesed.
What is the g sequence? I don't understand it
This was a great kata, I actually spent a week on it (was much harder than most 5th kyu that I've done!). I had decided to use the C++
list
type, because, oh I don't know, I guess I thought thesort()
remove()
andunique()
functions would be useful. But while submitting I learned that the code was timing out before completing the tests. Turns out,list
is too inefficient. I changed my type tovector
and everything was gravy.This is another case where I strongly recommend adding one of the big test cases to the sample test case section...
Took me a while to understand the descriptioin completely. Once I actually understand each series it wasn't too bad.
cann't submit in F# /home/codewarrior/program.fsx(92,30): error FS0039: The field, constructor or member 'Max' is not defined.
I think you got that only in the random tests. Please tell me if it works now. I added for the SUBMIT tests:
open System.Linq
. Thanks for your post!It works now, thanks. Also I was forced to create function 'gcd (x:int) (y:int): int'. It is used somewere in the submit tests.
I added a gcd function in the reference solution for random tests. Thanks again!
This was such a nasty problem, especially the
anOverAverage(n)
which I could never figure out in Haskell. Overall, a satisfying challenge.This comment has been hidden.
This comment has been hidden.
Oups! Sorry for the inconvenience:-( Suffix to gcdi function remained in my keyboard... Fixed (hopefully), if all is OK could you mark the issue as resolved?
Works now, thanks!
i have passed all test but when i trying attempt, it crash after complete 15 test. I dont understant why my code has this issues.
Very nice kata! Thank you, g964!
Thanks you so much!
Really nice Kata. It shows that prime number are cool and give us many funny exercises. Made in C++.
I've read here that instruction was written too poorly. From my point of view it was quite nice to read. I enjoy your Katas a lot.
Thanks for your post!
Sorry, this kata's description is just written too poorly. Additionally, I don't think the test cases are all correct (for JavaScript at least).
Consider this:
Looking at the primes in
gn(104)
give:Sorting and removing duplicates yields
So, if you want us to define a function
p(n)
that returns n unique primes, it might make sense whereHowever, if you look at the primes in
gn(105)
, you getRemoving duplicates and sorting yields
So in this case, using a larger
n
in ourgn(n)
function gave us more primes (in this case, it added 7). So who's to say thatp(6)
shouldn't beThat satifies the definition of "return n unique prime numbers."
Not knowing how to seed the
gn(n)
function gives us a non-deterministicp(n)
, which makesmaxp(n)
just trying to guess what you want.Not to mention, I have no idea what
anOver(n)
is supposed to do, as well asanOverAverage(n)
I must totally agree. Either I'm too stupid to understand this instruction or it's written too poorly. I wrote the code. I think it's correct. I think the result it gives back is correct (double checked it manually), but it's not (according to tests). I have no clue why and instruction does not clarifies this for me.
Agreed. Description is unintelligable to me. Will have to skip this one.
I agree, it is hard to understand what author actually expects from us for each function.
I don't understand also to
anOver(n)
andanOverAverage(n)
.This was a slog, and I don't feel like it was the best learning opportunity, especially when reviewing your solution. There were a lot of things that users can learn from a similar problem, such as memoization, or even algorithms relating to GCD (I didn't know there was a gcd function, so at least I learned something about the Euclidean method), but the problem was set up without those in mind. I also was not crazy about the fact that a 5kyu problem does not have an elegant solution. Hopefully you don't find this insulting, however (IMHO) the best Kata's are those that teach you something about algorithms, math concepts, or elegant writing, and this kata comes close in some regards, but just isn't there yet.
This kata was primarily written for Haskell and maybe not elegantly translated into Ruby. Hopefully you will soon author a kata about algorithms, math concepts and elegant writing. Nevertheless thanks for your post
Some very minor naming inconsistencies in the Python description, viz.
anOver(n)
vs.an_over(n)
.I see your point but the description is for all languages and not only for Python (functions in initial solution and tests are correctly spelled, I think, in Python and it is there that it is the most important). Sometimes I write something as "anOver(n) or an_over(n) or an-over(n) according to the language". Here the description was rather long with many functions and I found that all this writing was heavy and discouraging for the readers so I avoided it. As you know it is rather long to write katas in many languages but you could say that if I find it too tiring, I could not do it and you would be right...
Is not a fundamental question
What is your issue?
anOver(n) with parameter n: returns an array (n terms) of the a(n)/n for every n such g(n) != 1 (not tested but interesting result)
The n represents two different things which can be confusing. The n in anOver(n) represents the size of the array but the n in a(n)/n represents the index.
Maybe you could do something like anOver(n) with parameter n: returns an array (n terms) of the a(i)/i for every i such g(i) != 1 (not tested but interesting result)
Modified. Thanks!
Hello,
I completed this kata and the tests complete in about 3 seconds but every time I want to submit it I receive an error, because the program needed more than 10 seconds. Is my code to inefficient? When I run it on NetBeans it is actually pretty fast (under 1 second).
Which language? The "Submit" tests are in general far much longer than the "Your examples test cases".
In Java!
I think your code isn't efficient enough. Try to improve it:-)
O'Sensei, may I inquire of the maximum expected size of n? :)
100000 for countOnes, less for others.
I had an issue with function maxp. for n = 5, I have a sequence p: 5, 3, 11, 3, 23 and max of them is 23. But your test case expect to get value 47. 47 is a max value of p sequence, when it have only unique values.
May be you should add this information to description.
Fixed, thanks!
This is still an issue with the Haskell test cases.
Please can you be more precise, I don't see where the problem is:-(
Still having this problem in C#.
Can you post the test output?
well, the test case 'test2' expects 43, while the max item is 23. Exacly the above issue, but in the C# testcases
As far as I know for n = 5, test 2 expect 47. The fixed tests are the same in all languages and 175 guys passed the kata. Did you notice in the description:
Same problem in java, still for max(5), i get 47, which is not correct
I'm passing the test but trying to submit I get following error:
/tmp/haskell115924-17-1mg1dhh/Codewars/G964/Weirdgenprime/Test.hs:31:38: Not in scope:
frstSeries' Perhaps you meant
frstSeriesSol' (line 27)Woop! Fixed. Could you try again and mark the issue as resolved if, I hope, it works?
i'm still getting the same error
I had made the change but CW had taken no account! Now it should work.
solved, thanks
You are welcome, thanks for the feedback!