A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ganeshie8

  • one year ago

why does this function work ``` function is_composite(n){ return "1".repeat(n).match(/^1?$|^(11+?)\1+$/); } ``` https://jsfiddle.net/dy8t4vou/

  • This Question is Closed
  1. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    @ParthKohli

  2. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    or anyone

  3. dan815
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    woah lol what is that so cool

  4. dan815
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    what language?

  5. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    javascript, try it in ur browser console

  6. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    should be `\( \)`

  7. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    don't really know what you are trying to make it look like if it is a latex question....

  8. dan815
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    dang that looks so beautiful haha, i wish i knew what that syntax meant

  9. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    i can't help. don't really get the result you desire to get.

  10. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    screen shot or pic? (you can quite well draw it in colors and everything else with wio's extension for chrome which I am sure you have heard about...)

  11. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    whatever. won't interrupt.

  12. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    |dw:1435258573923:dw|

  13. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    @SolomonZelman

  14. dan815
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    how much of the syntax are u able to understand?

  15. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    you want to make that picture \(\normalsize\color{blue}{ 4 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) \(\normalsize\color{blue}{ 6 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) \(\normalsize\color{blue}{ 8 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) \(\normalsize\color{blue}{ 9 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) \(\normalsize\color{blue}{ 10 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) \(\normalsize\color{blue}{ 12 }\) \(\normalsize\color{blue}{ ^\text{______________________________________} }\) ??

  16. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    I'm able to follow it till "why does this function work"

  17. SolomonZelman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    (I am not very good at this)

  18. dan815
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    lool

  19. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Haha typical programmer from math background

  20. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    It's just a function that takes parameter "n" and returns "1" if the function is composite and "0" if it's not, i.e., it's prime.

  21. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    You got it! you're ready to use the function !

  22. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Looks like it checks if it contains certain keywords in its binary representation.

  23. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    thats very close, it actually converts the given number into unary number system

  24. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Or hex representation? I don't know.

  25. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Oh, unary...

  26. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    5 = 11111 3 = 111 etc

  27. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    "1".repeat(n) produces a string of "n" ones

  28. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    All the percentages and pluses and question-marks... oh dear.

  29. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    for simplicty, we can condence it further. below also works for n > 1 : ``` function is_composite(n){ return "1".repeat(n).match(/^(11+)\1+$/); } ```

  30. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    The problem is that I don't really know what that is. What I think here is that you should first generate the number (parameter n) by repeating 1 'n' times, then start a number that is not 1 or the number itself, keep repeating it and reach the parameter. If such a number exists, then the thing is composite.

  31. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Like it should definitely be a loop, but I don't know where it mentions it being a loop. What I see is that the .match is attached to the .repeat so does the .repeat function tell it to repeat or what?

  32. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    consider an example in unary system `1111` is composite becuase it can be split as `11 11`

  33. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `11111` is not composite because it cannot be split as 2 ones or 3 ones or 4 ones groups

  34. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Yeah, makes sense. Where does it say that in the program? :|

  35. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    match() is a string matching function

  36. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    it returns the match if it finds even groups of (11)+ otherwise it returns null

  37. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    ? looks like it's a conditional operator.

  38. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    for example : "parthkohli".match(/java/) returns null

  39. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    "parthkohli".match(/art/) returns true

  40. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    OK, I gotcha. Of course. This thing is looping over the size of the groups, right? So I'm sort of understanding this thing. I guess it says something like, "does it form even groups of this size? If not, proceed to the next size." Is that what (11)+ means?

  41. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `11+` I mean

  42. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Exactly!

  43. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    \(a+\) means 1 or more occurrences of \(a\)

  44. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `/^1?$|^(11+?)\1+$/` Could you walk us through each and every symbol above? Also, please do it quickly, because my battery is gonna finish in 10 minutes...

  45. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    lets look at the condensed expression `/^(11+?)\1+$/`

  46. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `^` matches start of string `$` matches end of string

  47. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `11+?` matches "two" or more occurrences of "1"

  48. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    `\1` backreferences the previous matched stuff in a parenthesis : `(11+?)`

  49. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    look up "regular expressions" for more details their syntax mostly same in almost all programming languages

  50. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    That is amazing. Wow.

  51. ParthKohli
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    OK, good night then. Laterz.

  52. ganeshie8
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    gnite!

  53. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.