Post History
APL (Dyalog Unicode), 33 bytes {×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺} Try it online! The first case doesn't work because it gets really big, but the other two do. The input is taken on the left a...
Answer
#2: Post edited
- # [APL (Dyalog Unicode)], 33 bytes
- <!-- language-all: lang-apl -->
- {×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺}
- [Try it online!][TIO-ko9a95i1]
- [APL (Dyalog Unicode)]: https://www.dyalog.com/
- [TIO-ko9a95i1]: https://tio.run/##JYwxbsJAEEV7n@KXtkQUdjfLYiRuwQVAxGsnSEayEVjgCgQhyUp0rmhS5QCJIqX1TeYiZmyaeaM/8/50uXiYF9NFapsmouNlW1cbBr3vfUmff@R@A79mfPuPPIL@eBeQ@68rcT@ONvR2YtLHgeOyaWzbwWtE7uzLzqTzV1sjyf0wS88TMhwggoTgSEF6npGweNIaSvUgBEQLXnpQMF0i20Cx2w8HQmmjFBtmCHbY4pvuvhgG5K6YxEmGl1WWwz7nGfI0xSyxSOavWMfFDQ "APL (Dyalog Unicode) – Try It Online"
The first case doesn't work because it gets really big, but the other two do. The input is taken on the left and the fractions are taken as a table on the right.
- # [APL (Dyalog Unicode)], 33 bytes
- <!-- language-all: lang-apl -->
- {×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺}
- [Try it online!][TIO-ko9a95i1]
- [APL (Dyalog Unicode)]: https://www.dyalog.com/
- [TIO-ko9a95i1]: https://tio.run/##JYwxbsJAEEV7n@KXtkQUdjfLYiRuwQVAxGsnSEayEVjgCgQhyUp0rmhS5QCJIqX1TeYiZmyaeaM/8/50uXiYF9NFapsmouNlW1cbBr3vfUmff@R@A79mfPuPPIL@eBeQ@68rcT@ONvR2YtLHgeOyaWzbwWtE7uzLzqTzV1sjyf0wS88TMhwggoTgSEF6npGweNIaSvUgBEQLXnpQMF0i20Cx2w8HQmmjFBtmCHbY4pvuvhgG5K6YxEmGl1WWwz7nGfI0xSyxSOavWMfFDQ "APL (Dyalog Unicode) – Try It Online"
- The first case doesn't work because it gets really big, but the other two do. The input is taken on the left and the fractions are taken as a table on the right.
- ```
- {×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺}
- ⍺× ⍝ n multiplied by
- 1⌷⍵ ⍝ The first row of the right argument (every p)
- (2⌷⍵) ⍝ Second row of right arg (all q's)
- | ⍝ All n×p modulo q
- 0= ⍝ Check which ones are 0 (rational)
- ÷⍨ ⍝ Make another vector of 'n×p÷q's
- /⍨ ⍝ And keep the ones that were rational
- ⊃ ⍝ Pick the first (0 if empty)
- x← ⍝ Assign to x
- × ⍝ Sign of x
- ⍺ ⍝ If sign is 0, return n
- x∇⍵ ⍝ Otherwise, call again with x as new n
- ```
#1: Initial revision
# [APL (Dyalog Unicode)], 33 bytes <!-- language-all: lang-apl --> {×x←⊃(2⌷⍵)(÷⍨(/⍨)0=|)⍺×1⌷⍵:x∇⍵⋄⍺} [Try it online!][TIO-ko9a95i1] [APL (Dyalog Unicode)]: https://www.dyalog.com/ [TIO-ko9a95i1]: https://tio.run/##JYwxbsJAEEV7n@KXtkQUdjfLYiRuwQVAxGsnSEayEVjgCgQhyUp0rmhS5QCJIqX1TeYiZmyaeaM/8/50uXiYF9NFapsmouNlW1cbBr3vfUmff@R@A79mfPuPPIL@eBeQ@68rcT@ONvR2YtLHgeOyaWzbwWtE7uzLzqTzV1sjyf0wS88TMhwggoTgSEF6npGweNIaSvUgBEQLXnpQMF0i20Cx2w8HQmmjFBtmCHbY4pvuvhgG5K6YxEmGl1WWwz7nGfI0xSyxSOavWMfFDQ "APL (Dyalog Unicode) – Try It Online" The first case doesn't work because it gets really big, but the other two do. The input is taken on the left and the fractions are taken as a table on the right.