Write numbers as a difference of Nth powers

24

4

Challenge

There are many numbers which can be expressed as the difference of two squares, or as the difference of two cubes, or maybe even higher powers. Talking about squares, there are various ways of writing a number, say 75, as the difference of 2 squares. You can write:

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

So let's talk about the challenge. Firstly, the user enters a number and then he enters a value for n. You need to display all the ways in which that number can be written in the form of aⁿ - bⁿ.

Input and Output

The input will be the number and the value of n. Your output shall have all such pairs of 'a' and 'b' such that the above-stated condition is met. The first number in the pair must be larger than the second. Please note that a, b, n and the input number are all positive integers, and n > 1.

Examples

50, 2 -> (none)

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

7, 3 -> (2,1)

665, 6 -> (3, 2)

81, 4 -> (none)

Scoring

This is , so the shortest code wins!

Manish Kundu

Posted 2018-01-14T15:06:30.543

Reputation: 1 947

Answers

9

Jelly, 8 bytes

p*ƓIFẹ+d

This is a monadic link that takes the number as argument and reads n from STDIN.

Try it online!

How it works

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].

Dennis

Posted 2018-01-14T15:06:30.543

Reputation: 196 637

6

Haskell, 42 bytes

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

Try it online!

Ungolfed with UniHaskell and -XUnicodeSyntax

import UniHaskell

f     ∷ Int → Int → [(Int, Int)]
f k n = [(a, b) | b ← 1 … k, a ← b … k, a^n - b^n ≡ k]

Can't change much else...

totallyhuman

Posted 2018-01-14T15:06:30.543

Reputation: 15 378

Actually, the equal sign == in UniHaskell is somewhat confusing, as it denotes congruence in mathematics. – user202729 – 2018-01-16T10:41:01.600

4

MATL, 11 bytes

t:i^&-!=&fh

Try it online! Or verify all test cases.

Explanation

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

Luis Mendo

Posted 2018-01-14T15:06:30.543

Reputation: 87 464

4

05AB1E, 9 bytes

Very inefficient for larger input values.

LãDImƹQÏ

Try it online!

Explanation

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

Emigna

Posted 2018-01-14T15:06:30.543

Reputation: 50 798

4

APL (Dyalog), 21 bytes

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

Try it online!

Left argument is n.

Uriel

Posted 2018-01-14T15:06:30.543

Reputation: 11 708

4

Python 2, 65 bytes

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

Try it online!

Dennis

Posted 2018-01-14T15:06:30.543

Reputation: 196 637

2

Python 3, 71 bytes

Thanks Mr.Xcoder for saving some bytes!

lambda x,n:[(a,b)for b in range(1,x)for a in[(b**n+x)**(1/n)]if a%1==0]

Try it online!


Python 3, 69 bytes

lambda x,n:[(a,b)for b in range(1,x)for a in range(x)if a**n-b**n==x]

Try it online!

Use totallyhuman's x^2 brute force approach actually saves bytes.

user202729

Posted 2018-01-14T15:06:30.543

Reputation: 14 620

77 bytes – Mr. Xcoder – 2018-01-14T15:18:19.773

3Unfortunately, brute forcing is usually the shortest approach. :P – totallyhuman – 2018-01-14T15:34:49.583

'b in range(x)' works on TIO. That makes 67 bytes. – Alix Eisenhardt – 2018-01-15T12:16:59.643

@AlixEisenhardt I don't think so.

– user202729 – 2018-01-15T12:55:35.147

2

Jelly, 10 bytes

*Iċ³
ṗ2çÐf

A full program taking i, and n which prints out the pairs of [b,a] with an empty output when there are none.

Try it online!

How?

*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

Jonathan Allan

Posted 2018-01-14T15:06:30.543

Reputation: 67 804

1Okay fine. You can keep it as you wish. – Manish Kundu – 2018-01-14T16:41:11.550

2

JavaScript (ES7), 64 bytes

A recursive function taking input in currying syntax (n)(p). Returns a space-separated list of pairs of integers, or an empty string if no solution exists. Uses the same algorithm as user202729's Python answer.

n=>p=>(g=x=>x--?((y=(x**p+n)**(1/p))%1?[]:[y,x]+' ')+g(x):[])(n)

Or 60 bytes with 0-terminated, encapsulated arrays:

n=>p=>(g=x=>x--&&((y=(x**p+n)**(1/p))%1?g(x):[y,x,g(x)]))(n)

This would output [ 9, 7, [ 6, 2, 0 ] ] for f(32)(2).

Test cases

let f =

n=>p=>(g=x=>x--?((y=(x**p+n)**(1/p))%1?[]:[y,x]+' ')+g(x):[])(n)

console.log(f(50)(2))
console.log(f(32)(2))
console.log(f(7)(3))
console.log(f(665)(6))

Arnauld

Posted 2018-01-14T15:06:30.543

Reputation: 111 334

2

Pyth, 14 bytes

f/.+^RvzTQ^SQ2

Try it here!, Alternative!

Mr. Xcoder

Posted 2018-01-14T15:06:30.543

Reputation: 39 774

1

Octave, 80 bytes

function r=f(n,p)[X Y]=meshgrid(1:n,1:n);r=[X(:) Y(:)](X(:).^p-Y(:).^p==n,:);end

Try it online!

Steadybox

Posted 2018-01-14T15:06:30.543

Reputation: 15 798

1

Perl 6, 45 bytes

->\k,\n{grep * **n-* **n==k,flat 1..k X 1..k}

Try it online!

Sean

Posted 2018-01-14T15:06:30.543

Reputation: 4 136