Psychology 9040a: Scientific Computing with MATLAB

Table of Contents

Practice Assignment #1 (practice 1)

You can use the input() function in MATLAB to query the user for input. You can use the disp() or fprintf() function in MATLAB to display output to the screen.

sample solution: practice1.m

Project Euler Problem 1 (pe001)

Create a variable to keep track of a running total. Initialize its value to zero. Then use a for loop to go through all integers up to some limit, and for each one, use an if statement to test whether each integer is divisible by 3 or by 5 with zero remainder. You can use the mod() function in MATLAB to find the remainder after division.

sample solution: pe001.m

Project Euler Problem 2 (pe002)

You can use a for loop to solve this.

sample solution: pe002.m

Project Euler Problem 6 (pe006)

You can use a for loop to solve this challenge. Use two variables to keep track of the sum of squares, and the squared sum. After the for loop is done, compute the difference between those two variables.

sample solution: pe006.m

Advent of Code 2015 Day 1 (aoc201501)

You can use the MATLAB function called fileread() to read in the puzzle input into an array of characters:

>> p = fileread('puzzle_input.txt');
>> p(1:10)

ans =

    '()(((()))('

sample solution: aoc201501.m

FizzBuzz (FizzBuzz)

Use the mod() function in MATLAB to find the remainder after division. Use a for loop to run through each number up to some maximum. Use an if statement to test each number. You can use the fprintf() or disp() function in MATLAB to print things to the screen.

sample solution: fizzbuzz.m

Primes (Primes)

A simple brute force method might be to use a for loop to test whether the candidate number is divisible with zero remainder by numbers starting from 2 up to the candidate number minus 1. If in no case is the answer "yes" then the number must be prime. You can use the mod() function in MATLAB to find the remainder after division.

There are various efficiencies you can implement to make your script run faster. Here are some hints. Do you always need to test all numbers between 2 and the candidate subtract one? Are even numbers ever prime? Do you need to run through every number between 2 and the candidate number minus one, or can you stop before that?

sample solution: primes.m

Project Euler Problem 7 (pe007)

Use a variable that you initialize before a for loop to keep track of how many primes you've found so far.

sample solution: pe007.m

Advent of Code 2015 Day 2 (aoc201502)

You can use the importdata() function in MATLAB to load the data file into memory:

boxes = importdata('ac201502_input.txt');

You can use the MATLAB function split() to split a string into consituent parts, given a separator character. You can use the str2num() function in MATLAB to convert a string into a number:

a_box = boxes{i};
sides = split(a_box, 'x');
l = str2num(sides{1});
w = str2num(sides{2});
h = str2num(sides{3});

You can use the min() function in MATLAB to find the minimum value of an array. You can use the sort() function in MATLAB to sort values in an array.

sample solution: aoc201502.m

Advent of Code 2015 Day 4 (aoc201504)

Here is a MATLAB function you can use to perform MD5 hashing. Copy and paste it into a new file, name it md5.m and then you can use md5() within MATLAB to compute the hash of a string:

function h = md5(s)

persistent MD5
if isempty( MD5 )
    MD5 = java.security.MessageDigest.getInstance( 'MD5' );
end

MD5.update( uint8( s(:) ) );
h = typecast( MD5.digest, 'uint8' );
h = dec2hex( h )';
h = lower( h(:) )';

end

The answer to Part 1, at least for my key (yzbqklnj), is 282749. Part 2 will take a looong time to run. For my key (yzbqklnj) the answer is 9962624. On my fast desktop it took about 13 minutes to run the MATLAB code.

sample solution: aoc201504.m

I also wrote a solution in C, compiled it, and it only took 9.8 seconds to run… an illustration of how much faster a compiled language like C can often be, compared to an interpreted language like MATLAB.

There is another MD5 routine you can try, which is roughly 10x faster, because it's compiled C code: GetMD5. Download it, unzip it, start MATLAB, navigate to the folder you just unzipped, and run InstallMex('GetMD5.c'). Then the GetMD5() function will be available to you.

Project Euler Problem 59 (pe059)

bitwise XOR in MATLAB

The MATLAB function bitxor() will perform the XOR operation on bytes of data. To encrypt a string of characters like 'hello!' you can first convert the character string into 8-bit integer bytes using the int8() function. Here is a small example of encrypting the character string 'hello!' using the encryption key 'me', repeated 3 times to match the number of characters of the string to be encrypted:

% encryption
plaintext = 'hello!';
key = 'me';
plaintext_bytes = int8(plaintext);
key_bytes = int8(key);
ciphertext_bytes = bitxor(plaintext_bytes, repmat(key_bytes, 1, 3));
disp(ciphertext_bytes)

Here is an example of decrypting the ciphertext using the (known) key:

% decryption

ciphertext_bytes = int8([5,0,1,9,2,68]);
key = 'me';
key_bytes = int8(key);
plaintext_bytes = bitxor(ciphertext_bytes, repmat(key_bytes, 1, 3));
plaintext = char(plaintext_bytes);
disp(plaintext)

Loading the cipher text

You can use the MATLAB function load() to load the cipher text. Assuming it's saved in a file called cipher.txt, you can use code like:

cipher = load('cipher.txt');

sample solution: pe059.m