MATLAB-190828-Euler Plan

Posted by unclemid on Wed, 28 Aug 2019 15:42:11 +0200

Write at the front
I am a beginner of MATLAB programming, this blog as a stage of a small exercise, welcome to communicate with me!
One sentence a week
Fast is slow, slow is fast. When you feel helpless in life, you are on the way up the hill. But when you feel relaxed in life, you may already be on the way down. Keep a positive attitude and embrace every day. Come on and become a vegetable chicken as soon as possible!
Topic 8

The meaning of the title is to find the maximum multiplied by 13 consecutive digits of the 1000 numbers.
thinking
At first I saw a series of numbers, which was a bit confusing. Think carefully, the most important thing is how to take out 13 digits and compare them until you find the maximum. The first step of my program is to treat 1000 numbers as strings, mainly because it can avoid being too large to express; the second step is to take 13 numbers from 1 - (1000-12) and multiply 13 numbers each time. I think this is the most difficult part of the program. You can use A=a(i:(i+12)) to take 13 characters at a time, and convert a string into an array by B=str2num(A(:)'. (Ps: The reason for this step can be understood as: A (:) turns a row of strings into a column of strings, then str2num into a column of arrays, through'transpose into a row of arrays', and then through prod(B). To derive the product of five elements. Finally, we can get the maximum value by comparing it all the time.
code block

disp('Finding the Maximum of the Product of 13-bit Continuous Numbers in 1000 Numbers:')
clear;       %A method of writing a long string in separate lines a=[''...''...]
a=['73167176531330624919225119674426574742355349194934'...
'96983520312774506326239578318016984801869478851843'... 
'85861560789112949495459501737958331952853208805511'... 
'12540698747158523863050715693290963295227443043557'... 
'66896648950445244523161731856403098711121722383113'... 
'62229893423380308135336276614282806444486645238749'... 
'30358907296290491560440772390713810515859307960866'... 
'70172427121883998797908792274921901699720888093776'... 
'65727333001053367881220235421809751254540594752243'... 
'52584907711670556013604839586446706324415722155397'... 
'53697817977846174064955149290862569321978468622482'... 
'83972241375657056057490261407972968652414535100474'... 
'82166370484403199890008895243450658541227588666881'... 
'16427171479924442928230863465674813919123162824586'... 
'17866458359124566529476545682848912883142607690042'... 
'24219022671055626321111109370544217506941658960408'... 
'07198403850962455444362981230987879927244284909188'... 
'84580156166097919133875499200524063689912560717606'... 
'05886116467109405077541002256983155200055935729725'... 
'71636269561882670428252483600823257530420752963450']
t=1;
max=1;
%%b=a(m:n)Used to extract strings m reach n Bit string
for i=1:1000-12      %From the first number of strings to 1000-12
    m=a(i:(i+12));   %Take out a 13-bit string
    s=m(:);          %Line up strings
    n=str2num(s)';   %The purpose of this statement is to convert strings into arrays
    t=prod(n);       %number l Multiplication of the elements of a group
    if t>max
        max=t;
    end
end
fprintf('The product of the largest 13-bit continuous number of outputs:%d\n',max);

Result
The product of the largest 13-bit continuous number of outputs: 23514624000
Finally, I would like to welcome you to criticize and correct the code I wrote, and share with you!

Topics: MATLAB Programming