计算逆模量


18

任务:

输出for的值x,其中a mod x = b有两个给定值a,b。

假设条件

  • a并且b将始终为正整数
  • 永远不会有解决方案 x
  • 如果存在多个解决方案,请至少输出其中之一。
  • 如果没有任何解决方案,则不输出任何内容或表明不存在任何解决方案。
  • 允许内置(不像其他数学方法那样有趣)
  • 输出始终是整数

例子

A, B >> POSSIBLE OUTPUTS

5, 2 >> 3
9, 4 >> 5
8, 2 >> 3, 6
6, 6 >> 7, (ANY NUMBER > 6)
8, 7 >> NO SOLUTION
2, 4 >> NO SOLUTION
8, 5 >> NO SOLUTION
10,1 >> 3, 9

这是,因此最低字节获胜。


如果找不到解决方案,会出错吗?
— 拍手

@ConfusedMr_C从技术上讲,这确实表示没有解决方案,所以是的。
— 重力1998年

Answers:


11

JavaScript,28 27 26 24 23字节

a=>b=>(a-=b)?a>b&&a:b+1

在线尝试!

false 表示没有解决方案。

-1感谢@Arnauld


做得很好,打高尔夫球也很好。
— 毛茸茸的

因此,要调用它,您需要给外部函数命名,例如f=...,然后调用f(8)(3)?似乎有点作弊?调用函数的通常方法是f(8,3),这会使您的函数定义更长?
— 史蒂夫·贝内特

3
@SteveBennett诚然,定义咖喱,这意味着它必须调用一样(8)(3),但有是是是允许的上PPCG共识。您不必给它起个名字。
— eush77

1
@SteveBennett有趣的是,您认为26 vs -15 只是一个明确的共识。祝你好运,试图引起争议。
— eush77

1
@SteveBennett 55:1是一个弱共识?
— Cyoce

6

MATL,6个字节

tQ:\=f

在线尝试!或验证所有测试用例。

说明

考虑输入8,2作为一个例子。

t    % Implicit input. Duplicate                STACK: 8, 8
Q    % Add 1                                    STACK: 8, 9
:    % Range                                    STACK: 8, [1 2 3 4 5 6 7 8 9]
\    % Modulo                                   STACK: [0 0 2 0 3 2 1 0 8]
=    % Implicit input. Equality comparison      STACK: [0 0 1 0 0 1 0 0 0]
f    % Indices of nonzeros. Implicit display    STACK: [3 6]



3

Groovy,48个字节(使用内置):

{a,b->Eval.me(a+"g").modInverse(Eval.me(b+"g"))}

Eval.me(...+"g") -在输入后加上“ g”,使其成为BigInteger。

modInverse(...) -执行逆模运算。


Java 8,70字节

{a,b->return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(b));}

3

R,33 28字节

pryr::f(match(b,a%%1:(a+1)))

在线尝试!

-4个字节感谢Jarko Dubbeldam。

-1个字节,感谢Giuseppe。

NA如果没有解决方案,则返回。TIO没有安装pryr库,因此该链接处的代码function(a,b)代替了。


pryr::f(which(a%%1:(a+1)==b)) 短4个字节。
— JAD

您还可以通过使用丢弃另一个字节match(b,a%%1:(a+1)),该字节将返回NA缺少的值。
— 朱塞佩


1

Mathematica 36字节

a_±b_:=Select[Range[9a],a~Mod~#==b&]

输入:

5 ± 2
9 ± 4
8 ± 2
6 ± 6
8 ± 7
2 ± 4
8 ± 5
10 ± 1

输出:

{3}
{5}
{3, 6}
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, \
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, \
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54}
{}
{}
{}
{3, 9}

当以二进制形式使用这些扩展的ASCII运算符时,在定义它们时需要在其前面加一个空格,否则解析器将引发错误。因此必须如此a_ ±b_。但是无论如何,使用它Cases代替Select一个未命名的函数会更短:Cases[Range[9#],x_/;#~Mod~x==#2]&
— Martin Ender


1

C#(Mono C#编译器),57 56 26字节

Rod的Python答案端口。 感谢WW的-1个字节。

非常感谢Kevin Cruijssen的-30个字节。

a=>b=>a-b>b?a-b:a==b?a+1:0

在线尝试!


1
欢迎光临本站!看来您可以在之后删除空间return。
— 小麦巫师

欢迎来到PPCG!对于非递归C#答案,使用lambda函数(i=>{/*code here*/})总是最好和最短的。但是,在这种情况下,由于您有2个输入,因此可以使用lambda函数来节省一个额外的字节(a=>b=>{/*code here*/}而不是(a,b)=>{/*code here*/})。另外,您可以删除if-check周围的括号。总的来说,在不更改任何功能的情况下,它变为a=>b=>a-b>b?a-b:a==b?a+1:0 26个字节
— Kevin Cruijssen

另外,如果您还没有看过,那么<all language> 打高尔夫的技巧和C#打高尔夫的技巧可能都很有趣。入住愉快!:)
— Kevin Cruijssen

谢谢大家的提示,将来在打高尔夫球时我会记住这些提示。
— Epicness '18





0

公理,147128字节

g(a:PI,c:PI):Union(List PI,Stream INT)==(a<c=>[];r:=a-c;r=0=>expand((a+1..)::UniversalSegment INT);[b for b in divisors(r)|b>c])

解开并测试

--a%b=c return all possible b
f(a:PI,c:PI):Union(List PI, Stream INT)==
    a<c=>[]
    r:=a-c
    r=0=>expand((a+1..)::UniversalSegment INT)
    [b  for b in divisors(r)|b>c]

(3) -> [[i,j,g(i,j)] for i in [5,9,8,6,8,2,8,10] for j in [2,4,2,6,7,4,5,1]]
   (3)
   [[5,2,[3]], [9,4,[5]], [8,2,[3,6]], [6,6,[7,8,9,10,11,12,13,14,15,16,...]],
    [8,7,[]], [2,4,[]], [8,5,[]], [10,1,[3,9]]]
                                                      Type: List List Any

这将找到所有解决方案,甚至是无限集解决方案...


0

点,9字节

a%,a+2@?b

将两个数字作为命令行参数。输出最小的解,如果没有解,则输出nil。在线尝试!

说明

           a, b are cmdline args (implicit)
  ,a+2     Range from 0 up to but not including a+2
a%         Take a mod each of those numbers
           (Note that a%0 returns nil; it emits a warning, but only if warnings are turned on)
      @?b  Find the index of the first occurrence of b in this list, or nil if it doesn't occur
           Autoprint (implicit)

例如,对于输入8和2:

   a+2   10
  ,      [0 1 2 3 4 5 6 7 8 9]
a%       [() 0 0 2 0 3 2 1 0 8]

2此列表中第一次出现的基于0的索引是3,这是我们的解决方案。







0

ORK,566字节

When this program starts:
I have a inputter called I
I have a number called a
I have a number called b
I is to read a
I is to read b
I have a mathematician called M
M's first operand is a
M's second operand is b
M is to subtract
I have a number called n
n is M's result
M's first operand is b
M's second operand is n
M is to compare
I have a scribe called W
If M says it's less then W is to write n
If M says it's less then W is to write "\n"
M's second operand is a
M is to compare
If M says it's equal then W is to write a
If M says it's equal then W is to write a

在线尝试!

ø bjects ř ķ OOL。幸运的是,我无需为此任务使用任何工具(内置工具除外)。


0

F#,40个字节

let m a b=Seq.find(fun x->a%x=b){1..a+1}

在线尝试!

很简单。System.Collections.Generic.KeyNotFoundException如果找不到解决方案,则抛出a 。

您也可以将其修改为Seq.tryFind,如果找不到解决方案,则会返回int option,并带有None。





0

私语v2,128字节

> Input
> Input
>> 1²
>> (3]
>> 1%L
>> L=2
>> Each 5 4
>> Each 6 7
>> L⋅R
>> Each 9 4 8
> {0}
>> {10}
>> 12∖11
>> Output 13

在线尝试!

返回一组所有可能的解决方案,以及空集(即 ∅)(如果不存在解决方案)。

怎么运行的

毫不奇怪,它的工作原理几乎与大多数其他答案相同:它生成一个数字列表,并使用参数检查每个数字的反模数。

如果您熟悉Whispers程序结构的工作原理,请随时跳到水平线。如果不是:从本质上讲,Whispers从最后一行开始在逐行参考系统上工作。每行被分类为两个选项之一。它是一条空白线,还是一条操作员线。

Nilad行以开头>,例如> Input或,> {0}并返回该行表示的确切值,即> {0}返回集合{0}。> Input返回STDIN的下一行,如果可能,则进行评估。

运算符行以开头>>,例如>> 1²或>> (3],表示在一个或多个值上运行运算符。在这里,所使用的数字不引用那些显式数字,而是引用该行上的值。例如,²是square命令(ñ→ñ2),因此>> 1²不返回值1个2,而是返回第1行的平方,在这种情况下,这是第一个输入。

通常,操作员行仅使用数字作为参考,但您可能已经注意到了>> L=2和>> L⋅R。这两个值L和R与Each语句一起使用。Each语句通过使用两个或三个参数(又作为数字引用)来工作。第一个参数(例如5)是对使用函数的运算符行的引用,其余参数是数组。然后,我们在数组上迭代该函数,其中的L和R在函数中表示要迭代的数组中的当前元素。举个例子:

让 一种=[1个,2,3,4], B=[4,3,2,1] and f(x,y)=x+y. Assuming we are running the following code:

> [1, 2, 3, 4]
> [4, 3, 2, 1]
>> L+R
>> Each 3 1 2

We then get a demonstration of how Each statements work. First, when working with two arrays, we zip them to form C=[(1,4),(2,3),(3,2),(4,1)] then map f(x,y) over each pair, forming our final array D=[f(1,4),f(2,3),f(3,2),f(4,1)]=[5,5,5,5]

Try it online!


How this code works

Working counter-intuitively to how Whispers works, we start from the first two lines:

> Input
> Input

This collects our two inputs, lets say x and y, and stores them in lines 1 and 2 respectively. We then store x2 on line 3 and create a range A:=[1...x2] on line 4. Next, we jump to the section

>> 1%L
>> L=2
>> Each 5 4
>> Each 6 7

The first thing executed here is line 7, >> Each 5 4, which iterates line 5 over line 4. This yields the array B:=[i%x|i∈A], where a%b is defined as the modulus of a and b.

We then execute line 8, >> Each 6 7, which iterates line 6 over B, yielding an array C:=[(i%x)=y|i∈A].

For the inputs x=5,y=2, we have A=[1,2,3,...,23,24,25], B=[0,1,2,1,0,5,5,...,5,5] and C=[0,0,1,0,0,...,0,0]

We then jump down to

>> L⋅R
>> Each 9 4 8

which is our example of a dyadic Each statement. Here, our function is line 9 i.e >> L⋅R and our two arrays are A and C. We multiply each element in A with it's corresponding element in C, which yields an array, E, where each element works from the following relationship:

Ei={0Ci=0AiCi=1

We then end up with an array consisting of 0s and the inverse moduli of x and y. In order to remove the 0s, we convert this array to a set (>> {10}), then take the set difference between this set and {0}, yielding, then outputting, our final result.


-1

C#, 53 bytes (83 with function heading)

static int F(int a, int b){
    for(int i=1;i<=a+1;i++){if(a%i==b)return i;}return 0;
}

Try It Online

First try at codegolf. Probably not the best language to use, nor the most efficient coding.


5
Remove the unnecessary whitespace to save about 10 or more bytes
— Mr. Xcoder
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.