regex to match irreducible fractions

后端 未结 4 1008
野的像风
野的像风 2021-02-04 01:11

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 02:12

    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:

    • delete every 0369
    • change 47 to 1, (because 4%3 and 7%3 = 1)
    • change 58 to 2, reason see above
    • change every 2 to 11
    • change every group of 111 to nothing.

    If the result is empty, the number was divisible by 3:

    echo ${RANDOM}${RANDOM}${RANDOM} | sed 's/[0369]//g;s/[47]/1/g;s/[58]/2/g;s/2/11/g;s/1\{3\}//g'
    

    A similar approach could work for 9, where you have a similar rule. But a general approach for arbitrary divisors?

提交回复
热议问题