# Bulgarian solitaire is a game in which you start with a sequence of...

## Question

Bulgarian solitaire is a game in which you start with a sequence of numbers that adds to k(k+1)/2 for some k. (This sum is 1 + 2 + … + k.) Think of each number in the sequence as representing a collection of pebbles. (For example, the sequence <6, 9> represents two piles containing 6 and 9 pebbles.) Form a new sequence by taking one pebble from each pile to form a new pebble. Thus for example, the sequence <6, 9> generates the sequence <2, 5, 8>, then <1, 3, 4, 7> etc. The sequence stops with <1, 2, 3, 4, 5> since this sequence remains unchanged after a move.
The problem is the following: Given a sequence, write a Matlab program that takes such a sequence as input and displays all the sequences generated by moves of Bulgarian solitaire until the sequence <1, 2, …, k> is reached for some k.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

clc
% Get input of sequence
sequence = str2num(input('Enter sequence of numbers, separated by spaces: ', 's'));

% Loop until completion
while 1
% Display sequence
disp(sequence);
% Copy last sequence into variable
last = sequence;
% Create total variable
total = 0;
% Iterate over sequence
for i = length(sequence):-1:1
% If the value is positive
if sequence(i) > 0...

By purchasing this solution you'll be able to access the following files:
Solution.m.

\$30.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available MATLAB for Computer Science Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.