Aparna Sarkar
Test 2
PHYS 108
11/22/2013

1) 
function result = month(length,start)
A=cell(7,5);
count=1;
    for j=start:numel(A)
        if count<=length
        A(j)={count};
        count = count +1;
        end
    end
result = transpose(A);    

>> month(30,5)
ans = 
      []      []      []      []    [ 1]    [ 2]    [ 3]
    [ 4]    [ 5]    [ 6]    [ 7]    [ 8]    [ 9]    [10]
    [11]    [12]    [13]    [14]    [15]    [16]    [17]
    [18]    [19]    [20]    [21]    [22]    [23]    [24]
    [25]    [26]    [27]    [28]    [29]    [30]      []

>> month(28,3)
ans = 
      []      []    [ 1]    [ 2]    [ 3]    [ 4]    [ 5]
    [ 6]    [ 7]    [ 8]    [ 9]    [10]    [11]    [12]
    [13]    [14]    [15]    [16]    [17]    [18]    [19]
    [20]    [21]    [22]    [23]    [24]    [25]    [26]
    [27]    [28]      []      []      []      []      []


4)
function problem_four
fid1 = fopen('results.txt', 'r');
if fid1 <= 0; fprintf('error in opening\n'); return; end
I = textscan(fid1,'%d64 %d64');
I= cell2mat(I);
close = fclose(fid1);
if close ~= 0; fprintf('error in closing\n'); return; end
sortedValues = unique(I(:,2));
maxValues= sortedValues(end-4:end);
commend =[];
pass=[];
fail=[];
commend = I(I(:,2)>=min(maxValues));
fid2=fopen('commend.txt', 'w');
    for k=1:length(commend)
        fprintf(fid2,'%s\n', int2str(commend(k)));
    end
close = fclose(fid2);
if close ~= 0; fprintf('error in closing\n'); return; end
pass = I(I(:,2)>50 & I(:,2)<min(maxValues));
fid3=fopen('pass.txt', 'w');
    for k=1:length(pass)
        fprintf(fid3,'%s\n', int2str(pass(k)));
    end
close = fclose(fid3);
if close ~= 0; fprintf('error in closing\n'); return; end
fail = I(I(:,2)<=50);
fid4=fopen('fail.txt', 'w');
    for k=1:length(fail)
        fprintf(fid4,'%s\n', int2str(fail(k)));
    end
close = fclose(fid4);
if close ~= 0; fprintf('error in closing\n'); return; end
end


commend.txt:
7501956331
1544240612
6911739018
9484632858
5852138185
8936798504
4320248914
2608895680

pass.txt:
1643009181
5694848582
1870570232
8363336984
8357923828
2348788982
6936447276
5667354482
9756770992
6840923434
8202975178
5084179378
4891523534
8427824158
1751228333
2198539068
2560497518
4518440220
8482417685
8230279524
4593319935
5741882474
4751195211
6651760232
3627856719
4884860532
1139384130
9856573519
2504515689
1955947104
4351687660
2783065622
5407188742
4055440720
9564674182
9282988358
1474092979
3422074837
4805520535
4759696938
9847472198
3713094538
7309888803
6997049664
7282949681
6998751220
2603192089
2152129597
9991723552
2540089597
1293407384
6050798134
2713899404
5146533435
2407644570
8699705252
6802880831
4386449892
2718313257
5338198549
2085504519
6305567362
3035689117
4461572119
6246877444
3266255102
3613965978
6553817959
8419386400
9843970597
7572239130
4094893037
6256623999
1969921137
9156773355
8916883520
8359845034
3346551991
6349206255
1202613334
3814469981
2453362698
4805971201
1848064049
6386713018


fail.txt:
7640722859
5930838110
7022577740
9834741558
4854276936
3387528188
4827333881


7)
function [result] = isfibonacci(n)
%assuming n is an integer
N1= 5*n^2-4;
N2= 5*n^2+4;
if(n>=0 && sqrt(N1)-floor(sqrt(N1))==0 || sqrt(N2)-floor(sqrt(N2))==0)
    result = true;
else result = false;
end
end


>> isfibonacci(5)
ans =
     1
>> isfibonacci(0)
ans =
     1
>> isfibonacci(89)
ans =
     1
>> isfibonacci(90)
ans =
     0
>> isfibonacci(134)
ans =
     0
>> isfibonacci(3)
ans =
     1
>> isfibonacci(4)
ans =
     0

8) 
function [result] = curious_property( )
for i=3:2e6
factsum=0;    
    stri=num2str(i);
    for j = 1:length(stri)
        factsum=factsum+factorial(str2double(stri(j)));
    end 
    if factsum==i && factsum~=145
        result = i;
        break;
    end      
end
end


>> curious_property
ans =
       40585



