5 kyu

Weird prime generator

31 of 728g964
Description
Loading description...
Fundamentals
Mathematics
  • Please sign in or sign up to leave a comment.
  • nomennescio Avatar

    Description issue: description is contradictory w.r.t to p it has:

    Removing the 1s gives a third sequence: p: 5, 3, 11, 3, 23, 3... where you can see prime numbers.
    

    as you can see, the third sequence has repeated primes. However the description also says:

    4:  p(n) with parameter n: returns an array filled with the n first distinct primes in the same order they are found in the sequence gn (not tested)
    

    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.

  • MarkShcerbakov Avatar

    Very interesting Kata!!! But description hmmmm... =) Thanks to author!!!

  • Quark Fox Avatar

    weird it is!!

  • ejini战神 Avatar
    • 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)

  • ejini战神 Avatar

    Ruby 3.0 should be enabled

  • ejini战神 Avatar

    count_ones && over_average function should also be tested in random tests of all languages

  • dfhwze Avatar

    Swift 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 :)

  • dfhwze Avatar

    p(n) is tested in Coffeescript.

    Testing maxPn: 
    ReferenceError: pn is not defined
        at maxPnSol
        at /home/codewarrior/test.coffee:98:43
        at begin
        at Object.it
        at /home/codewarrior/test.coffee:97:18
        at /runner/frameworks/javascript/cw-2.js:152:11
        at Promise._execute
        at Promise._resolveFromExecutor
        at new Promise
        at Object.describe
        at /home/codewarrior/test.coffee:63:14
        at /home/codewarrior/test.coffee:37:5
        at Object.handleError
                <anonymous>
    
  • seleemdz Avatar

    The second sequence g(i) is defined as g(i) = a(i) - a(i-1) for i >= 2 with g(1) = 1 !

  • fesilver Avatar

    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

  • SunMaster Avatar

    This comment has been hidden.

  • SunMaster Avatar

    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) ?

  • SunMaster Avatar

    Doing the C version

      2: gn(n) with parameter n: returns the first n terms of the series of g(n) (not tested)
      4: p(n) with parameter n: returns an array filled with the n first distinct primes in the same order they are found in the sequence gn (not tested)
      5: maxPn(n) with parameter n: returns the biggest prime number of the above p(n) # (tested)
      
    

    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.

  • nutel Avatar

    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!

  • RealKenshiro Avatar

    The description is too vague about p(n).

    p(n) with parameter n: returns an array of n unique prime numbers
    

    p(n) returns an array filled with the n first distinct primes in the same order they are found in the sequence gn.

  • user9644768 Avatar

    Please use new python test framework.

  • Greatlemer Avatar

    Hi, After solving this, I believe there are a couple of typos that should be corrected in the description:

    3: countOnes(n) with parameter n: returns the number of 1 in g(n)

    Shouldn't this be the number of 1s in gn(n)? g(n) is a single number, whilst gn(n) is the sequence.

    4: p(n) with parameter n: returns an array of n unique prime numbers (not tested)

    Similarly should this be pn(n) which you refer to in point 5, but never define?

  • rajat004 Avatar

    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

  • QuentinLR Avatar

    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.

  • unadamlar Avatar

    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.

  • OlegTar Avatar

    What is the g sequence? I don't understand it

  • wdkaye Avatar

    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 the sort() remove() and unique() 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 to vector 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...

  • erichlf Avatar

    Took me a while to understand the descriptioin completely. Once I actually understand each series it wasn't too bad.

  • usix Avatar

    cann't submit in F# /home/codewarrior/program.fsx(92,30): error FS0039: The field, constructor or member 'Max' is not defined.

  • KenKamau Avatar

    This was such a nasty problem, especially the anOverAverage(n) which I could never figure out in Haskell. Overall, a satisfying challenge.

  • mariusz.wisniewski Avatar

    This comment has been hidden.

  • tanluutrong2206 Avatar

    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.

  • cesare63 Avatar

    Very nice kata! Thank you, g964!

  • PiotrGrzybowski Avatar

    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.

  • heyitsmattwade Avatar

    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:

    [5, 3, 11, 3, 23, 3, 47, 3, 5, 3, 101, 3]
    

    Sorting and removing duplicates yields

    [3, 5, 11, 23, 47, 101]
    

    So, if you want us to define a function p(n) that returns n unique primes, it might make sense where

    p(6) = [3, 5, 11, 23, 47, 101];
    

    However, if you look at the primes in gn(105), you get

    [5, 3, 11, 3, 23, 3, 47, 3, 5, 3, 101, 3, 7]
    

    Removing duplicates and sorting yields

    [3, 5, 7, 11, 23, 47, 101];
    

    So in this case, using a larger n in our gn(n) function gave us more primes (in this case, it added 7). So who's to say that p(6) shouldn't be

    p(6) = [3, 5, 7, 11, 23, 47];
    

    That satifies the definition of "return n unique prime numbers."

    Not knowing how to seed the gn(n) function gives us a non-deterministic p(n), which makes maxp(n) just trying to guess what you want.

    Not to mention, I have no idea what anOver(n) is supposed to do, as well as anOverAverage(n)

  • alexandersmanning Avatar

    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.

  • ecolban Avatar

    Some very minor naming inconsistencies in the Python description, viz. anOver(n) vs. an_over(n).

  • JKDos Avatar

    Is not a fundamental question

  • renzhentaxi Avatar

    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)

  • julian_shr Avatar

    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).

  • noxnox Avatar

    O'Sensei, may I inquire of the maximum expected size of n? :)

  • Orbit3v1 Avatar

    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.

  • zamcl Avatar

    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)