0% found this document useful (0 votes)
45 views3 pages

Untitled 3

The document describes a MATLAB script that generates 10 random numbers of 30 digits each and performs various calculations on them. It calculates sums and alternating sums of the digits, as well as sums of 2-digit and 3-digit parts of the numbers, checking their divisibility by specific integers. The results are displayed in a table format indicating which numbers are divisible by the specified divisors.

Uploaded by

Bemain Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Untitled 3

The document describes a MATLAB script that generates 10 random numbers of 30 digits each and performs various calculations on them. It calculates sums and alternating sums of the digits, as well as sums of 2-digit and 3-digit parts of the numbers, checking their divisibility by specific integers. The results are displayed in a table format indicating which numbers are divisible by the specified divisors.

Uploaded by

Bemain Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

clc;

n_numbers=10;
length_of_numbers=30;
numbers=strings(n_numbers,1);
results=zeros(n_numbers,8);

for i=1:n_numbers
num1=num2str(randi([1,9],1),'%d');
num2=num2str(randi([0 9], 1, length_of_numbers-1), '%d');
numbers(i) = [num1,num2];
end
disp('Randomly generated numbers:');
disp(numbers);

sumat=zeros(1,10);
dsum=zeros(1,10);
for i=1:n_numbers
ar=[];
ar=num2str(numbers(i));
for j=1:length_of_numbers
sumat(i)=sumat(i)+str2num(ar(j));
dsum(i)=dsum(i)+str2num(ar(j))*(-1)^(j+1);
end
end

sum_of_2parts=zeros(1,10);
dsum_of_2parts=zeros(1,10);
for i=1:10
part=0;
numb=char(numbers(i));
k=1;
for j = [Link]-1
part = str2double(numb(j:j+1)); % Extract 3-digit part and
convert to number
sum_of_2parts(i) = sum_of_2parts(i) + part;
dsum_of_2parts(i)=dsum_of_2parts(i)+part*(-1)^(k+1);
k=k+1;
end
end

sum_of_3parts=zeros(1,10);
dsum_of_3parts=zeros(1,10);
for i=1:10
part=0;
numb=char(numbers(i));
k=1;
for j = [Link]-2
part = str2double(numb(j:j+2)); % Extract 3-digit part and
convert to number
sum_of_3parts(i) = sum_of_3parts(i) + part;
dsum_of_3parts(i)=dsum_of_3parts(i)+part*(-1)^(k);
k=k+1;

1
end
end

for i=1:10
if mod(sumat(i),3)==0
results(i,1)=1;
end
if mod(dsum_of_3parts(i),7)==0
results(i,2)=1;
end
if mod(sumat(i),9)==0
results(i,3)=1;
end
if mod(dsum(i),11)==0
results(i,4)=1;
end
if mod(dsum_of_3parts(i),13)==0
results(i,5)=1;
end
if mod(sum_of_3parts(i),37)==0
results(i,6)=1;
end

if (results(i,3) && results(i,4))==1


results(i,7)=1;
end

if mod(dsum_of_2parts(i),101)==0
results(i,8)=1;
end
end

divisors=[3,7,9,11,13,37,99,101];

disp(array2table(results, 'VariableNames', arrayfun(@(x)


['Divisible_by_', num2str(x)],divisors, 'UniformOutput', false)));

Randomly generated numbers:


"548349091656368009624122313180"
"930377568030177850982597844527"
"563143776105399288589575046539"
"883589626046984749983427895518"
"528782661427288346865347870644"
"283233553356974810013305016895"
"955344080483688912855685244966"
"735515648734377469771354028080"
"752516500104865809151507899521"
"557065195496485563258414348433"

Divisible_by_3 Divisible_by_7 Divisible_by_9


Divisible_by_11 Divisible_by_13 Divisible_by_37
Divisible_by_99 Divisible_by_101

2
______________ ______________ ______________
_______________ _______________ _______________
_______________ ________________

0 0 0 0
0 0 0
0
0 1 0 0
0 0 0
0
1 0 1 0
0 0 0
0
0 0 0 0
0 0 0
0
0 0 0 0
0 0 0
0
0 0 0 0
0 0 0
0
0 0 0 0
0 0 0
0
0 0 0 1
0 0 0
0
0 0 0 0
0 1 0
0
0 0 0 0
0 0 0
0

Published with MATLAB® R2018b

You might also like