# Chapter6

Problem 1
Script file:

%problem 1
a = 6*4 > 32 -3
b = 4*3-7<15/3>-1
c = 2*(3<8/4+2)^2<(-2)^3
d = (5+~0)/3==3 – ~(10/5-2)

Problem 2
Script file:

%problem 2
d = 6; e = 4; f = -2;
y1 = d+f>=e>d-e
y2 = e>d>f
y3 = e -d <= d -e == f/f
y4 = (d/e*f<f)>-1*(e-d)/f

Problem 3
Script file:

%problem 3
v = [-2 4 1 0 2 1 2];
w = [2 5 0 1 2 -1 3];
a = ~v==~w
b = w>=v
c = v>~-1*w
d = v> -1*w

Problem 4
Script file:

%problem 4
u = v(v<=w)

Problem 5
Script file:

%problem 5
a = 0|7&9&-3
b = 7>6&~0<=2
c = ~4<5|0>=12/6
d = -7<-5<-2&2+3<=15/3

Problem 6
Script file:

% problem 6 page book 193
ans6 = zeros(4,6);
for ii = 1:4%looping form 1-4, ii= rows, jj= columns
for jj = 1:6
ans6(ii, jj) = 2*ii – 3*jj;
end
end
ans6

%for j=1:3
%for k=1:5
%matrix(j,k)=j^k/(j+k);
%end
%end
%matrix

Problem 7
Script file:

%problem 7
% rem returns 1 if there is a remainder on the division, returns 0 if it
% is divisible
% clear
% clc
vector=round(40*rand(1,30)-20)
d = 0;
for k=1:30
if(~rem(vector(k),3))
d = d + vector(k);
end
end

disp(‘The sum of all elements that are divisible by 3 is: ‘)
disp(d)

Problem 8
Script file:

%problem 8
clear
% use [] brackets to enter array elements ex. [1 2 3]
vector = input(‘Please enter any array of integers of any length: ‘)
vectord = vector;
for k= 1:length(vector)
if(vector(k)>0)
vectord(k) = vector(k) * 2;

elseif (vector(k)< 0)
vectord(k) = vector(k) * 3;

end
end
fprintf(‘Positive elements now doubled and negative elements are tripled: \n’)
disp(vectord)

Problem 9
Script file:

%problem 9
clear
vector = input(‘Please enter any array of integers of any lenght: ‘)
n = length(vector);
j = 0;
for i = 1:n
if vector(i)>= 0

j = j + 1;
vNew(j) = vector(i);

end
end
elim = n – j;
fprintf(‘The number of eliminated elements is %.0f \n’, elim)
disp(vNew)

%Problem 9 alternative

v = input(‘Please input a vector: ‘);
[l, n] = size(A);
x = v;
for k = 1:n
if A(k)>=0
x(k)=v(k);
elseif v(k)<=0
x(k)=v(k)*0;
end
end
vnew=x(x>0);
fprintf(‘Unmodified Vector:\n’)
v
fprintf(‘Modified Vector:\n’)
vnew

Problem 10
Script file:

%Problem 10
NYC =[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34 44 21 23 30 39];
DEN =[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57 20 32 50 48 28];
%part (a)
NYCav = round(mean(NYC));
DENav = round(mean(DEN));
fprintf(‘The average temperature for the month in New York City is: % d F.\n’, NYCav);
fprintf(‘The average temperature for the month in Denver is: % d F.\n’, DENav);
%part(b)
nNYC = sum(NYC >NYCav);
nDEN = sum(DEN >DENav);
fprintf(‘During % g days the temperature in New York City was above the average.\n’, nNYC);
fprintf(‘During % g days the temperature in Denver was above the average.\n’, nDEN);
%part(c)
DENhNYC = sum(DEN > NYC);
fprintf(‘During % g days the temperature in Denver was higher than in New York City.\n’, DENhNYC);

%Problem 10 alternative solution
NYC =[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34 44 21 23 30 39];
DEN =[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57 20 32 50 48 28];

daysNYC=0;
sumNYC=0;
[m NYCLength]=size(NYC);
for j=[1:NYCLength]
sumNYC=NYC(j)+sumNYC;
end
NYCAVG=((sumNYC)/NYCLength);
fprintf(‘The average Temperature in NYC is: %.0f\n’,round(NYCAVG))
for i=[1:NYCLength]
if(NYC(i)>NYCAVG)
daysNYC=daysNYC+1;
end
end
fprintf(‘The number of days above average in NYC is: %d\n\n’,daysNYC)

daysDEN=0;
sumDEN=0;
[m DENLength]=size(DEN);
for j=[1:DENLength]
sumDEN=DEN(j)+sumDEN;
end
DENAVG=((sumDEN)/DENLength);
fprintf(‘The average Temperature in DEN is: %.0f\n’,round(DENAVG))
for i=[1:DENLength]
if(DEN(i)>DENAVG)
daysDEN=daysDEN+1;
end
end
fprintf(‘The number of days above average in Denver is: %d\n\n’,daysDEN)

DENaboveNYC=0;
for j=[1:NYCLength]
if DEN(j)>NYC(j)
DENaboveNYC = DENaboveNYC+1;
end
end
fprintf(‘The days DEN is Above NYC is: %.0f\n’,DENaboveNYC)

Problem 11
Script file:

% problem 11
n = input(‘What is the number of rows for Pascals triangle’);
P = abs(pascal(n,1))

Alternative code:

n=input(‘Enter the N dimension of the nxn Matrix of the Pascal Triangle:’);
k=n;
m=n;
x=[ones(n)];
for k = [1:n]
for m = [1:n]
if k-m<0
x(k,m)=0;
else
x(k,m)=(factorial(k-1))/((factorial(m-1)*factorial(k-m)));
end
end
end

x

Problem 12
Script file:

%problem 12
%clear
%clc
n = [0:24];
fibonacci(n)

Solution using a for loop

Fibonacci = [0,1,1];%the fisrt 3 elements of the sequence are 0,1,1
for k = 4:25%loking for the fisrt 25… I will start the look at the 4th element
Fibonacci(k) = Fibonacci(k-1)+Fibonacci(k-2); %each number in the sequence is the sum of the prevoius two
end
Fibonacci

Problem 13
Script file:

% Problem 13
n = [10 50 100];
f(1) = 1; f(2) = 1;
for x = 1:3
s = 2;
for y = 3:n(x)
f(y) = f(y – 1) + f(y -2);
s = s + 1 / f(y);
end
fprintf(‘The sum after %i terms is: %.12f\n’,n(x),s)
end

Problem 14
Script file:

%problem 14
%n = [10 100 1000];
format long
n = input(‘Enter the number of terms: ‘);
s =0;
for k = [0:n]
s =(((-1)^k)/((2*k+1)^(3))) + s;

end
x=(32*s)^(1/3);
fprintf(‘Pi is:\n’)
disp(pi)
fprintf(‘The sum of the series is:\n’)
disp(x)

Alternative solution:

format long
n=input(‘Enter the Value of n:’);
pie=0;
for i= 0:n
pie=(power(-1,i)/power((2*i+1),3))+pie;
end
Calculatedpi=(32*pie)^(1/3);
fprintf(‘This is the Real Pi:\n’)
disp(pi)
fprintf(‘This is the Calculated Pi for %i terms is:\n’, n)
disp(Calculatedpi)

Problem 15
Script file:

%problem 15
n=input(‘Please enter the Number of terms to be calculated:’)
format long
a(1)=sqrt(2)/2;
b=a(1);
for i=[2:n]
a(i)=sqrt(2+2*a(i-1))/2;
b=b*a(i);
end
x=2/b;
fprintf(‘The Real Value of pi is:\n’)
disp(pi)
fprintf(‘\nThe estimated value of pi with n terms is:\n’)
disp(x)

Alternative solution:

format long
n = [5 10 40];
for j = 1:3
t(1) = sqrt(2)/2;
T = t(1);
for k = 2:n(j)
t(k) = sqrt(2 + 2*t(k-1))/2;
T = T*t(k);
end
Est(j) = 2/T;
end
disp(‘pi =’)
disp(pi)
disp(‘Results for 5, 10 snd 40 terms are:’)
for j = 1:3
disp(Est(j))
end

Problem 16
Script file:

%problem 16
N=20;k =1:N;
while true
v(k)=randi([10 30],1,N);
if all(mod(v,2)==0) %b = mod(a,m) returns the remainder after division of a by m; (all)Determine if all array elements are nonzero or true
break;
else
k =find(mod(v,2)~=0); %Find indices and values of nonzero elements
N=numel(k);
v(k)=0;
end
end
v

Problem 17
Script file:

%problem 17
format shortG
x = [9 -1.5 13.4 13.3 -2.1 4.6 1.1 5 -6.1 10 0.2];
for k = 1:length(x)-1
for m = k +1 : length(x)
if x(m) < x(k)
temp = x(k);
x(k) = x(m);
x(m) = temp;
end
end
end
x

Problem 18
Script file:

%problem 18
x = 1;
for k = 1 : 50
for m = k + 1 : 50
for n = m + 1 : 50;
if n^2 == k^2 + m ^2
a(x) = k;
b(x) = m;
c(x) = n;
x = x + 1;
end
end
end
end
table = [a’ b’ c’]

Problem 19
Script file:

%problem 19
fprintf(‘Problem 19\n’)
x = 100:999;
a =mod(mod(x,100),10);
temp =round((rem(rem(x/10,100),10)),10);
b = temp – mod(temp,1);
temp =round((rem(rem(x/100,100),10)),10);
c = temp – mod(temp,1);
for k = 1:900
if a(k).*b(k).*c(k) == 6.*(a(k)+b(k)+c(k))

x1(k) = c(k)*100 + b(k)*10 + a(k);
else
x1(k) = 0;
end
end
v = x1(x1>0);
fprintf(‘Numbers between 100 and 999, whose product of digits is 6 times the sum of the digits is :\n’)
disp(v)

Problem 20
Script file:

%problem 20

k = 1;
for n = 1:500;
nispr = 1;
nsafeisPrime = 0;
%deternime if n is prime
for i = 2:fix(n/2)
if rem(n,i) == 0
nispr = 0;
break
end
end
if nispr == 1
nsafe = 2*n+1;
nsafeisPrime = 1;
for j = 2:fix(nsafe/2)
if rem(nsafe,j) == 0
nsafeisPrime = 0;
break
end
end
end
if nsafeisPrime == 1
k = k + 1;
end
end

Problem 21
Script file:

N=62; primes = 0; k = 0; f = 0;

while size(primes,2) < N
k = k + 1;

if k == 1
out = 0;
elseif k == 2
out = 1;
elseif all(rem(k,2:k-1) ~= 0)
out = 1;
else
out = 0;
end
if out == 1
f = f+1;
primes(f) = k;
end
end

topIndex = 2;
bottomIndex = 1;
resultIndex = 1;
while topIndex < N
if primes(topIndex) – primes (bottomIndex) == 6
result(resultIndex,1) = [primes(bottomIndex)];
result(resultIndex,2) = [primes(topIndex)];
resultIndex = resultIndex + 1;
topIndex = topIndex + 1;
bottomIndex = bottomIndex +1;
elseif primes(topIndex) – primes (bottomIndex) < 6
topIndex = topIndex + 1;
else
bottomIndex = bottomIndex +1;
end
end
disp(result)

Alternative:

N=62; primes = 0; k = 0; f = 0;

while size(primes,2) < N
k = k + 1;

if k == 1
out = 0;
elseif k == 2
out = 1;
elseif all(rem(k,2:k-1) ~= 0)
out = 1;
else
out = 0;
end
if out == 1
f = f+1;
primes(f) = k;
end
end

n=0;
f=0;
k=1;
while n<62
f=f+1;
for G=1:N
if primes(f)==primes(G)-6
SexyPrimes1(k)=primes(f);
SexyPrimes2(k)=primes(G);
k=k+1;
else

end
end
n=n+1;
end
SexyPrimes=[SexyPrimes1′ SexyPrimes2′]

Problem 22
Script file:

N=10000; primes=0; k=0; j=0;

while k<N
k=k+1;
if k==1
out=1;
elseif k==2
out=1;
elseif all(rem(k,2:k-1)~=0)
out=1;
else
out=0;
end
if out==1
j=j+1;
primes(j)=k;
end
end

h=0;
[n SizeOfPrimes]=size(primes);
for n=[1:13]
PossibleMersennePrime=(2^n)-1;
i=1;
while i<SizeOfPrimes
if primes(i)==PossibleMersennePrime
h=h+1;
MersennePrimes(h)=primes(i);
end
i=i+1;
end
end
MersennePrimes= MersennePrimes(MersennePrimes>1)

Alternative solution:

for k = 1:50000 %calculates mersenne primes using mersenne formula
p(k) = 2^k -1;
end
p = p(p<10000 & p>1);
psize =size(p);
x = psize(2);
mersenneprime = 0; k = 2; f = 0;

for m = 1:x %verifies if mersenne are prime numbers
k = k +1;
a = p(m);
b = p(m)-1;
if all((rem(a,2:b)) ~= 0)
flag = 1;
else
flag = 0;
end
if flag == 1
f = f+1;
mersenneprime(f) = p(m);
end
end %it eliminates 7 numbers from original array
mersenneprime

Problem 23
Script file:

n=1;
num=4;
x=0;
while n<=4
num=num+1;
k=1;
for j=1:(num/2)
if rem(num,j)==0
x(k)=j;
k=k+1;
end
end
if sum(x)==num
perfectNumbers(n)=num;
n=n+1;
end
x=0;

end
perfectNumbers

Problem 24
Script file:

%problem 24
S=[72 81 44 68 90 53 80 75 74 65 50 92 85 69 41 73 70 86 61 65 79 94 69];
[n, numExams]=size(S);
Average=sum(S)/(numExams);
Av = round(Average)

sum=0;
for i=1:numExams
sum=((S(i)-Average)^2)+sum;
end
sum1=sum/numExams;
Sd = round(sqrt(sum1))

for i=1:numExams
if S(i)>(Av+(1.3*Sd))
elseif S(i)>(Av+(0.5*Sd))
elseif S(i)>(Av-(0.5*Sd))
elseif S(i)>(Av-(1.3*Sd))
elseif S(i)<(Av-(1.3*Sd))
end
end

Problem 25
Script file:

n=0;
fprintf(‘If the value is in the a^x format\n’)
E=1;
sum=0;

while E>0.000001
sumMin1=sum;
sum=(log(a)^n)/(factorial(n))*(x^n)+sum;

E=abs(((sum)-(sumMin1))/(sumMin1));

n=n+1;
end
fprintf(‘The Value is:%.12f\n’,sum)

Problem 26
Script file:

%problem 26
k = 1;
S = 1;
while S<1000
S=k*(k+1)/2;
num1=floor(S/100);
num2=floor((S-num1*100)/10);
num3=floor(S-num1*100-num2*10);
if num1==num2 & num2==num3
break
end
k = k +1;
end
fprintf(‘The desired sum is %i\n’, S)
fprintf(‘This is the sum of the first %i digits\n’, k)

Problem 27
Script file:

%problem 27
if x == 1
INTEN = 0.55;
elseif x == 2
INTEN = 0.65;
else
INTEN = 0.8;
end

if Gender == 1
MHR=(203.7)/(1+(exp((0.033*(Age-104.3)))));
else Gender == 2
MHR=(190.2)/(1+(exp((0.0453*(Age-107.5)))));

end
THR=(MHR-RHR)*INTEN+RHR;

fprintf(‘Your Training Heart Rate is: %.0f\n’,THR)

Problem 28
Script file:

%problem 28
for j=1:2
BMI=703*W/h^2;
if BMI<18.5
fprintf(‘\nYour BMI value is %.1f, which classifies you as underweight\n\n’,BMI)
elseif BMI<25
fprintf(‘\nYour BMI value is %.1f, which classifies you as normal\n\n’,BMI)
elseif BMI<30
fprintf(‘\nYour BMI value is %.1f, which classifies you as overweight\n\n’,BMI)
else
fprintf(‘\nYour BMI value is %.1f, which classifies you as obese\n\n’,BMI)
end
end

Problem 29
Script file:

%problem 29
TypeOfCar=input(‘Please enter the type of car (0-Sedan) (1-SUV):’);
NumberOfDays=input(‘Please enter the Number of Days:’);
NumberOfMilesDriven=input(‘Please enter the number of miles driven:’);

if TypeOfCar==0
if NumberOfDays<=6
cost=(79*NumberOfDays)+((NumberOfMilesDriven-80)*0.69);
end
if NumberOfDays<=29
cost=(69*NumberOfDays)+((NumberOfMilesDriven-100)*0.59);
end
if NumberOfDays>30
cost=(59*NumberOfDays)+((NumberOfMilesDriven-120)*0.49);
end
end

if TypeOfCar==1
if NumberOfDays<=6
cost=(84*NumberOfDays)+((NumberOfMilesDriven-80)*0.74);
end
if NumberOfDays<=29
cost=(74*NumberOfDays)+((NumberOfMilesDriven-100)*0.64);
end
if NumberOfDays>30
cost=(64*NumberOfDays)+((NumberOfMilesDriven-120)*0.54);
end
end

fprintf(‘The Cost of the Rental is: \$%.2f\n’,cost)

Problem 30
Script file:

%problem 30
for j=1:3
n(1:8)=0;
cost=randi([1 5000],1,1)/100;
fprintf(‘The total charge is \$%.2f\n’,cost)
pay=input(‘Please enter payment (1, 5, 10, 20, or 50): ‘);
if pay<cost
fprintf(‘Insufficient Payment\n\n’)
continue
else
change = pay – cost;
if change >=20
n(1)=1;
change=change-20;
end
if change>=10
n(2)=1;
change=change-10;
end
if change>=5
n(3)=1;
change=change-5;
end
while change>=1
n(4)=n(4)+1;
change=change-1;
end
while change>=.25
n(5)=n(5)+1;
change=change-.25;
end
while change>=.10
n(6)=n(6)+1;
change=change-.10;
end
if change>=.05
n(7)=1;
change=change-.05;
end
change=change+.000001;
while change>=.01
n(8)=n(8)+1;
change=change-.01;
end
end

if n(1)>0
if n(1) == 1
fprintf(‘one \$20 bill, ‘)
elseif n(1) == 2
fprintf(‘two \$20 bills, ‘)
end
end

if n(2)>0
if n(2) == 1
fprintf(‘one \$10 bill, ‘)
elseif n(2) == 2
fprintf(‘two \$10 bills, ‘)
end
end

if n(3)>0
if n(3) == 1
fprintf(‘one \$5 bill, ‘)
elseif n(3) == 2
fprintf(‘two \$5 bills, ‘)
end
end

if n(4)>0
if n(4) == 1
fprintf(‘one \$1 bill, ‘)
elseif n(4) == 2
fprintf(‘two \$1 bills, ‘)
elseif n(4) == 3
fprintf(‘three \$1 bills, ‘)
elseif n(4) == 4
fprintf(‘four \$1 bills, ‘)
end
end

if n(5)>0
if n(5) == 1
fprintf(‘one quater, ‘)
elseif n(5) == 2
fprintf(‘two quaters, ‘)
elseif n(5) == 3
fprintf(‘three quaters, ‘)

end
end

if n(6)>0
if n(6) == 1
fprintf(‘one dime, ‘)
elseif n(6) == 2
fprintf(‘two dimes, ‘)
elseif n(6) == 3
fprintf(‘three dimes, ‘)

end
end

if n(7)>0
if n(7) == 1
fprintf(‘one nickle, ‘)
elseif n(7) == 2
fprintf(‘two nickles, ‘)
elseif n(7) == 3
fprintf(‘three nickles, ‘)

end
end

if n(8)>0
if n(8) == 1
fprintf(‘one penny.\n\n’)
elseif n(8) == 2
fprintf(‘two pennies.\n\n’)
elseif n(8) == 3
fprintf(‘three pennies.\n\n’)
elseif n(8) == 4
fprintf(‘four pennies.\n\n’)

end
end
fprintf(‘\n\n’)
end