7 kyu

T.T.T.27: Four piles of apples

219 of 517myjinxin2015

Description:

Problem

There are n apples that need to be divided into four piles. We need two mysterious number x and y. Let The number of first pile equals to x+y, the number of second pile equals to x-y, the number of third pile equals to x*y, the number of fourth pile equals to x/y. We need to calculate how many apples are there in each pile.

Of course, there won't be so many unknowns. We know the total number of apples(n) and the second mysterious number(y).

For example: there are 48 apples need to divided into four piles. y=3. that is, 1st pile should be x+3, 2nd pile should be x-3, 3rd pile should be x*3, 4th pile should be x/3. Do you know how much x is? x should be 9, because:

(9 + 3) + (9 - 3) + (9 * 3) + (9 / 3) = 12 + 6 + 27 + 3 = 48

So, 48 apples should be divided into 12, 6, 27, 3.

Task

Implement a function that accepts two argument, n and y, and returns the number of apples in each pile as described above. Each resulting number should be a positive integer. If there's no way to divide the apples, return an empty array/empty value (refer to the function declaration and test cases in your language of choice to see which option is relevant for you).

Examples

n = 48
y = 3
result = [12, 6, 27, 3]

n = 100
y = 4
result = [20, 12, 64, 4]

n = 24
y = 4
result = []  (no way to divide the apples)

n = 25
y = 4
result = []  ([8,0,16,1] is not a correct answer since you can't have empty piles)
Puzzles
Games

Stats:

CreatedAug 10, 2016
PublishedAug 10, 2016
Warriors Trained992
Total Skips15
Total Code Submissions3808
Total Times Completed517
JavaScript Completions209
Haskell Completions48
PHP Completions54
Ruby Completions32
Crystal Completions10
Python Completions219
Total Stars13
% of votes with a positive feedback rating87% of 152
Total "Very Satisfied" Votes121
Total "Somewhat Satisfied" Votes21
Total "Not Satisfied" Votes10
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • GiacomoSorbi Avatar
  • smile67 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • Voile Avatar
  • ice1000 Avatar
  • FArekkusu Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad