How can I match irreducible fractions with regex?
For example, 23/25, 3/4, 5/2, 100/101, etc.
First of all, I have no idea about the gcd-algorithm realization in regex.
Update for all of you who's answering like "You are using the wrong tool":
Yeah, guys, I'm realizing what regex is normally used for. It's okay. But that this question is weird is kind of its whole point.
Updated 2: The idea is to find a regex that could be helpful in a situation like:
$> echo "1/2" | grep -P regex
1/2
$> echo "2/4" | grep -P regex
So, the regex should be only a string, without using any scripts and variables. Only regex.
Actually, I already know some regex which match reducible fractions written in the unary number system.
$> echo "11/1111" | grep -P '^1/1+$|(11+)+\1+/\1+$'
11/1111
So the thing is to convert from decimal to unary number system in regex, but I don't know how.
Nope it cannot be done. Like a good computer scientist I will ignore the specifics of the tool regex and assume you are asking if there is a regular expression. I do not have enough knowledge about regex's features to ensure it is restricted to regular expressions. That caveat aside, on with the show.
Rewording this we get:
Assume such a language is regular. Then there exists a DFA that can decide membership in
L
. LetN
be the number of states of such a DFA. There are an infinite number of primes. As the number of primes is infinite, there are arbitrarily many primes greater than the largest number encodable inN
digits in the radixr
. (Note: The largest number is clearlyr
raised to the power ofN
. I am using this weird wording to show how to accommodate unary.) SelectN+1
primes that are greater than this number. All of these numbers are encoded using at leastN+1
digits (in the radixr
). Enumerate these primesp₀
topₙ
. Letsᵢ
be the state of thepᵢ
is in immediately after reading the/
. By the pigeon hole principle, there areN
states andN+1
sᵢ
states so there exists at least one pair of indexes(j,k)
such thatsⱼ = sₖ
. So starting from the initial state of the DFA, inputspₖ/
andpⱼ/
lead to the same statesⱼ
(orsₖ
) andpⱼ
andpₖ
are distinct primes.L
must accept all pairs of distinct primesp/q
as they are coprime and reject all primes divided by themselvesp/p
asp
is not coprime top
. Now the language acceptspⱼ = pₖ
so there is a sequence of states fromsⱼ
using the stringpₖ
to an accepting state, call this sequenceβ
. Letα
be the sequence of states readingpₖ
starting from the initial state. The sequence of states for the DFA starting at the initial state for the stringpₖ/pₖ
must be the same asα
followed byβ
. This sequence starts in an initial state, goes tosₖ
(by reading the inputpₖ
), and reaches an accepting state by readingpₖ
. The DFA acceptspₖ/pₖ
andpₖ/pₖ
is inL
.pₖ
is not coprime topₖ
, and thereforepₖ/pₖ
is not inL
. Contradiction. Therefore the languageL
is irregular, or no regular expression exists.If you write the numbers in unary, and use ":" as the division sign, I think this matches reducible fractions:
You can then use !~ to find strings that don't match.
Based on this: http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
You can know, that a number, ending in (0,5) is divisible by (5), or ending in (2,4,6,8,0) is divisible by 2.
For 3,4,6,7,8,9 as divisors, I wouldn't expect a possibility, and not for arbitrary divisors too.
I guess you know the method, to decide divisibility by 3 - to build the rekursive crosssum, which has to be divisible by 3, to make the number divisible. So there you could eliminate all 3s, 6s and 9s from the number, as well as the 0. For an arbitrary number, you would proceed this way:
If the result is empty, the number was divisible by 3:
A similar approach could work for 9, where you have a similar rule. But a general approach for arbitrary divisors?
UPDATE
Since the poster requested a single regex that matches against strings like "36/270", but says it doesn’t matter how legible it is, that regex is:
But, if like me, you believe that an illegible regex is absolutely unacceptable, you will write that more legibly as:
You can improve maintainability by splitting out the version that matches the unary version from the one that matches the decimal version like this:
Isn’t that much easier by separating it into two named regexes? That would now make
$reducible_rx
the same as$decimal_rx
, but the unary version is its own thing. That’s how I would do it, but the original poster wanted a single regex, so you’d have to interpolate the nested one for that as I first present above.Either way, you can plug into the test harness below using:
And you will see that it is a correct regex that passes all tests, and does so moreover using a single regex, wherefore having now passed all requirements of the original question, I declare Qᴜᴏᴅ ᴇʀᴀᴛ ᴅᴇᴍᴏɴsᴛʀᴀɴᴅᴜᴍ: “Quit, enough done.”