ganeshie8
  • ganeshie8
why does this function work ``` function is_composite(n){ return "1".repeat(n).match(/^1?$|^(11+?)\1+$/); } ``` https://jsfiddle.net/dy8t4vou/
Discrete Math
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
ganeshie8
  • ganeshie8
@ParthKohli
ganeshie8
  • ganeshie8
or anyone
dan815
  • dan815
woah lol what is that so cool

Looking for something else?

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

More answers

dan815
  • dan815
what language?
ganeshie8
  • ganeshie8
javascript, try it in ur browser console
SolomonZelman
  • SolomonZelman
should be `\( \)`
SolomonZelman
  • SolomonZelman
don't really know what you are trying to make it look like if it is a latex question....
dan815
  • dan815
dang that looks so beautiful haha, i wish i knew what that syntax meant
SolomonZelman
  • SolomonZelman
i can't help. don't really get the result you desire to get.
SolomonZelman
  • SolomonZelman
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...)
SolomonZelman
  • SolomonZelman
whatever. won't interrupt.
ganeshie8
  • ganeshie8
|dw:1435258573923:dw|
ganeshie8
  • ganeshie8
@SolomonZelman
dan815
  • dan815
how much of the syntax are u able to understand?
SolomonZelman
  • SolomonZelman
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{______________________________________} }\) ??
ParthKohli
  • ParthKohli
I'm able to follow it till "why does this function work"
SolomonZelman
  • SolomonZelman
(I am not very good at this)
dan815
  • dan815
lool
ganeshie8
  • ganeshie8
Haha typical programmer from math background
ParthKohli
  • ParthKohli
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.
ganeshie8
  • ganeshie8
You got it! you're ready to use the function !
ParthKohli
  • ParthKohli
Looks like it checks if it contains certain keywords in its binary representation.
ganeshie8
  • ganeshie8
thats very close, it actually converts the given number into unary number system
ParthKohli
  • ParthKohli
Or hex representation? I don't know.
ParthKohli
  • ParthKohli
Oh, unary...
ganeshie8
  • ganeshie8
5 = 11111 3 = 111 etc
ganeshie8
  • ganeshie8
"1".repeat(n) produces a string of "n" ones
ParthKohli
  • ParthKohli
All the percentages and pluses and question-marks... oh dear.
ganeshie8
  • ganeshie8
for simplicty, we can condence it further. below also works for n > 1 : ``` function is_composite(n){ return "1".repeat(n).match(/^(11+)\1+$/); } ```
ParthKohli
  • ParthKohli
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.
ParthKohli
  • ParthKohli
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?
ganeshie8
  • ganeshie8
consider an example in unary system `1111` is composite becuase it can be split as `11 11`
ganeshie8
  • ganeshie8
`11111` is not composite because it cannot be split as 2 ones or 3 ones or 4 ones groups
ParthKohli
  • ParthKohli
Yeah, makes sense. Where does it say that in the program? :|
ganeshie8
  • ganeshie8
match() is a string matching function
ganeshie8
  • ganeshie8
it returns the match if it finds even groups of (11)+ otherwise it returns null
ParthKohli
  • ParthKohli
? looks like it's a conditional operator.
ganeshie8
  • ganeshie8
for example : "parthkohli".match(/java/) returns null
ganeshie8
  • ganeshie8
"parthkohli".match(/art/) returns true
ParthKohli
  • ParthKohli
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?
ParthKohli
  • ParthKohli
`11+` I mean
ganeshie8
  • ganeshie8
Exactly!
ganeshie8
  • ganeshie8
\(a+\) means 1 or more occurrences of \(a\)
ParthKohli
  • ParthKohli
`/^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...
ganeshie8
  • ganeshie8
lets look at the condensed expression `/^(11+?)\1+$/`
ganeshie8
  • ganeshie8
`^` matches start of string `$` matches end of string
ganeshie8
  • ganeshie8
`11+?` matches "two" or more occurrences of "1"
ganeshie8
  • ganeshie8
`\1` backreferences the previous matched stuff in a parenthesis : `(11+?)`
ganeshie8
  • ganeshie8
look up "regular expressions" for more details their syntax mostly same in almost all programming languages
ParthKohli
  • ParthKohli
That is amazing. Wow.
ParthKohli
  • ParthKohli
OK, good night then. Laterz.
ganeshie8
  • ganeshie8
gnite!

Looking for something else?

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