태그 보관물: math

math

N 제곱의 차이로 숫자 쓰기 숫자를 쓰는 여러 가지 방법이 있습니다

도전

두 제곱의 차이 또는 두 큐브의 차이 또는 더 높은 거듭 제곱으로 표현할 수있는 숫자가 많이 있습니다. 정사각형에 대해 말하면 2 정사각형의 차이로 숫자를 쓰는 여러 가지 방법이 있습니다 (75). 당신은 쓸 수 있습니다:

75 = (10)^2 - (5)^2
   = (14)^2 - (11)^2
   = (38)^2 - (37)^2

그럼 도전에 대해 이야기합시다. 먼저, 사용자는 숫자를 입력 한 다음 n에 대한 값을 입력합니다. 해당 번호가 aⁿ-bⁿ 형식으로 작성 될 수있는 모든 방법을 표시해야합니다.

입력과 출력

입력 값은 n의 숫자와 값입니다. 출력은 위에 언급 된 조건이 충족되도록 모든 ‘a’와 ‘b’쌍을 가져야합니다. 쌍의 첫 번째 숫자는 두 번째 숫자보다 커야합니다. 있습니다 A, B, N 및 상기 입력 번호가 모두 양의 정수이고,> 1 N .

50, 2 -> (none)

32, 2 -> (9,7), (6, 2)

7, 3 -> (2,1)

665, 6 -> (3, 2)

81, 4 -> (none)

채점

이것은 이므로 가장 짧은 코드가 승리합니다!



답변

젤리 , 8 바이트

p*ƓIFẹ+d

이것은 숫자를 인수로 사용하고 STDIN에서 n 을 읽는 모나드 링크입니다 .

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

작동 원리

p*ƓIFẹ+d  Main link. Argument: k

p         Cartesian product; yield all pairs [b, a] with b and a in [1, ..., k].
  Ɠ       Get; read an integer n from STDIN.
 *        Power; map each [b, a] to [b**n, a**n].
   I      Increments; map each [b**n, a**n] to [a**n-b**n].
    F     Flatten the resulting list of singleton arrays.
     ẹ    Every; find all indices of k in the list we built.
      +   Add k to the indices to correct the offset.
       d  Divmod; map each index j to [j/k, j%k].


답변

하스켈 , 42 바이트

k#n=[(a,b)|b<-[1..k],a<-[b..k],a^n-b^n==k]

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

와 Ungolfed UniHaskell-XUnicodeSyntax

import UniHaskell

f      Int  Int  [(Int, Int)]
f k n = [(a, b) | b  1  k, a  b  k, a^n - b^n  k]

다른 많은 것을 바꿀 수 없습니다 …


답변

05AB1E , 9 바이트

더 큰 입력 값에는 매우 비효율적입니다.

LãDImƹQÏ

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

설명

L           # range [1 ... input_1]
 ã          # cartesian product with itself
  D         # duplicate
   Im       # raise each to the power of input_2
     Æ      # reduce each pair by subtraction
      ¹QÏ   # keep only values in the first copy which are true in this copy


답변

MATL , 11 바이트

t:i^&-!=&fh

온라인으로 사용해보십시오!또는 모든 테스트 사례를 확인하십시오 .

설명

t     % Implicit input: M. Duplicate
:     % Range [1 2 ... M]
i     % Input: n
^     % Power, element-wise. Gives [1^n 2^n ... M^n]
&-    % Matrix of pairwise differences (size n×n)
!     % Transpose. Needed so the two numbers in each pair are sorted as required
=     % Is equal? Element-wise. Gives true for entries of the matrix equal to M
&f    % Row and column indices of true entries
h     % Concatenate horizontally. Implicit display


답변

APL (Dyalog) , 21 바이트

{o/⍨⍵=-/¨⍺*⍨¨o←,⍳2⍴⍵}

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

왼쪽 인수는 n입니다.


답변

파이썬 2 , 65 바이트

lambda k,n:[(j%k,j/k)for j in range(k,k*k)if(j%k)**n-(j/k)**n==k]

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


답변

젤리 , 10 바이트

*Iċ³
ṗ2çÐf

전체 프로그램 복용 i하고 n있는 쌍 밖으로 인쇄 [b,a]아무도 없다 빈 출력.

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

방법?

*Iċ³ - Link 1, isValid?: pair of +ve integers, [b,a]; +ve integer, n
*    - exponentiate             -> [b^n,a^n]
 I   - incremental differences  -> [a^n-b^n]
   ³ - program's third argument -> i
  ċ  - count occurrences        -> 1 if a^n-b^n == i, otherwise 0

ṗ2çÐf - Main link: +ve integer i, +ve integer n
ṗ2    - second Cartesian power = [[1,1],[1,2],...,[1,i],[2,1],...,[2,i],...,[i,i]]
   Ðf - filter keeping if:
  ç   -   call last link (1) as a dyad (left = one of the pairs, right = n)
      - implicit print of Jelly representation of the list