계수 검증 4 1,

모두가 참이고 두 개의 숫자와 결과를 갖는 모듈로 나머지 계산으로 구성된 수학 표현식의 목록이 주어지면 목록의 n모든 명령문에 대해 참인 첫 번째 숫자 를 산출 해야합니다.

예를 들면 다음과 같습니다.

[m % 3 = 0, m % 4 = 1, m % 5 = 3]여기서 %는 모듈로 연산자입니다.

들어 n= 3, 순서에 맞게 처음 3 개 숫자 (0에서 계산)이 있습니다 33, 93, 153, 따라서 귀하의 결과는 (당신까지 포맷)이 될 것입니다.

규칙 / IO

  1. 당신은 양수 n와 진리의 목록을 취합니다 . 물론 필요한 것은 모듈로 연산의 RHS와 결과입니다.
  2. n진리 목록의 숫자는 항상 1-> 2 ^ 31-1 범위 에 있으며 결과도 마찬가지입니다.
  3. 편리한 형태로 입력하고 편리한 형태로 출력합니다. 예를 들어, input : 3 [3 0, 4 1, 5 3]및 output : 33 93 153입니다.
  4. 솔루션이 수학적으로 가능하다는 것이 보장됩니다.
  5. 입력 소스는 파일, 함수 매개 변수, stdin 등에서 올 수 있습니다. 출력도 동일합니다.
  6. 허점이 없습니다.
  7. 이것은 코드 골프이므로 가장 낮은 바이트 수가 이깁니다.

테스트 케이스

# Input in the form <n>, <(d r), (d2 r2), ...>
# where <d> = RHS of the modulo expression and <r> the result of the expression. Output in the next line.

5, (3 2), (4 1), (5 3)
53 113 173 233 293

3, (8, 0), (13, 3), (14, 8)
120 848 1576

의사 코드에서 참조 구현

n = (an integer from stdin)
truths = (value pairs from stdin)
counter = 0

while n != 0 {
    if matches_criterias(counter, truths) {
        print counter
        n -= 1
    }

    counter += 1
}



답변

젤리 , 7 바이트

%⁼⁴
0ç#

이것은 전체 프로그램입니다. 인수는 제수, 목표 계수 및 솔루션 수의 순서로 나옵니다.

온라인으로 사용해보십시오!

작동 원리

0ç#  Main link.
     Left argument: D (array of divisors)
     Right argument: M (array of target moduli)
     Third argument: n (number of solutions)

0ç#  Execute the helper link with k = 0, 1, 2, ... as left argument and D as the
     right one until n of them return 1. Yield the array of matches.


%⁼⁴  Helper link. Left argument: k. Right argument: D

%    Compute k % d for each d in D.
 ⁼⁴  Compare the result with M.


답변

펄 6 , 33 바이트

{grep((*X%@^b)eqv@^c,0..*)[^$^a]}

시도 해봐

입력은 ( number-of-values, list-of-divisors, list-of-remainders )

넓히는:

{   # bare block lambda with placeholder parameters 「$a」 「@b」 「@c」

  grep(

    # WhateverCode lambda:
    (

      *        # the value being tested

      X%       # cross modulus

      @^b      # with the divisors ( second parameter )

    )

    eqv        # is that list equivalent with

    @^c        # the expected remainders ( third parameter )

    # end of WhateverCode lambda

    ,

    0 .. *     # Range of all Integers starting with 0

  )[ ^$^a ]    # grab up-to 「$a」 values ( first parameter )
               # ( 「^$a」 is the same as 「0 ..^ $a」 )
}


답변

자바 스크립트 (ES6), 71 68 바이트

a=>f=(n,m=0)=>n?a.some(x=>m%x[0]-x[1],++m)?f(n,m):[m,...f(n-1,m)]:[]

간단한 재귀 함수. 다음과 같이 배열에서 첫 번째와 n두 번째 카레를 사용하여 사용하십시오 .

g=a=>f=(n,m=0)=>n?a.some(x=>m%x[0]-x[1],++m)?f(n,m):[m,...f(n-1,m)]:[]
g([[3, 2], [4, 1], [5, 3]])(5)


답변

자바 스크립트 (ES6), 74 70 69 바이트

정수로의 입력을 받고, n그리고 어레이 a[modulo, remainder]구문을 무두질와 어레이 (n)(a).

n=>a=>eval('for(i=r=[];a.some(([b,c])=>i%b-c)||--n*r.push(i);i++);r')

테스트 사례


답변

하스켈, 47 바이트

n#l=take n[i|i<-[0..],all(\(d,r)->mod i d==r)l]

사용 예 : 3 # [(8,0),(13,3),(14,8)]-> [120,848,1576].


답변

파이썬, 67 바이트

lambda n,r:[k for k in range(2**32)if all(k%d==m for d,m in r)][:n]


답변

자바 스크립트 (ES6), 72 70 바이트

a=>g=(n,i,r=[],m=a.some(e=>i%e[0]^e[1]))=>n?g(n-!m,-~i,m?r:[...r,i]):r

조건 배열을 먼저 처리하고 결과 수를 두 번째로 조정합니다. 편집 : 0을 처리하지 않고 2 바이트를 절약했습니다.