Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need answers to both the questions in matlab If the numbers 1 to 5 are written o

ID: 3813663 • Letter: N

Question

Need answers to both the questions in matlab

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4+ 4 = 19 letters used in total. Write a function called number2letters that returns the number of letters needed to write down the number n in words. For example, 342 (three hundred forty two) contains 20 letters. Notice that we do not count spaces, nor do we use hyphens. The only input to the function is n, a positive integer smaller than 1000, but you do not need to check this. (Inspired by Project Euler.) Write a function called circular_primes that finds the number of circular prime numbers smaller than n, where n is a positive integer scalar input argument. For example, the number, 197, is a circular prime because all rotations of its digits: 197, 971, and 719, are themselves prime. For instance, there are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. It is important to emphasize that rotation means circular permutation not all possible permutations.

Explanation / Answer

%% problem number2letters.m:

function m = number2letters (n)

% A is a 3x10 array of the letter counts of the words used to form various numbers, with
% units in row 1, "teens" in row 2 and tens in row 3. For the supplied number, we first
% determine the number of units, tens and hundreds. These three values determine the
% words and thus the letter count to be returned.

A = [ 0 3 3 5 4 4 3 5 5 4; ...              % units
        3 6 6 8 8 7 7 9 8 8; ...              % "teens"
        0 0 6 6 5 5 5 7 6 6];                 % tens
for i = 1:n
    h = fix(i/100);
    t = fix(rem(i,100)/10);
    u = rem(i,10);
      if h>0,     m = A(1,h+1)+7;                % h 'hundred'
    else        m = 0;
    end
    switch t
      case 0,   m = m+A(1,u+1);                % units only
      case 1,   m = m+A(2,u+1);                % "teens" only
      otherwise m = m+A(3,t+1)+A(1,u+1);       % tens and units
    end
end
end

%% problem circular_primes.m:

function n = circular_primes(k)
    n = 0;
    for p = primes(k-1)
        if circular_prime(p)
            n = n + 1;
        end
    end
end

function yes = circular_prime(k)
    d = num2str(k);
    for ii = 1:length(d)
        d = circshift(d,[0 1]);   % try next circular shifted version
        if ~isprime(str2num(d))
            yes = false;          % not circular prime
            return;               % so we can return
        end
    end
    yes = true;                   % if we made it here, it is a circular prime
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote