A list is created by writing all the positive integers from 1 to 3999 inclusively in Roman numerals in order.
Devise an algorithm that isolates all the perfect squares from the list and writes the corresponding square roots in Roman numerals.
Assume that:
- The algorithm will process a Roman Numeral directly without converting it to decimal when determining if it is a perfect square.
- The algorithm knows how to derive the square root of an integer.
- The algorithm is aware of the corresponding base ten value of all the six individual Roman numerals I, V, X, L, C, D, and M. For example, it knows that: X=10, C=100, D=500 and, so on.
- The algorithm knows the corresponding base ten value of a given Roman numeral representation. For example, it knows that: XVI = 10+5+1 =16
This just creates the list, in memory:
%create list
romanList=string.empty;
for i=1:3999
r=string(num2roman(i));
romanList(i)=r;
end
The list is sampled to make sure it's doing what it should do:
%sample the list
disp((romanList(85:101))')
The sample of 85 through 101:
"LXXXV"
"LXXXVI"
"LXXXVII"
"LXXXVIII"
"LXXXIX"
"XC"
"XCI"
"XCII"
"XCIII"
"XCIV"
"XCV"
"XCVI"
"XCVII"
"XCVIII"
"XCIX"
"C"
"CI"
The algorithm:
for sr=1:sqrt(3999)
fprintf('%12s %12s\n',romanList(sr*sr), romanList(sr));
end
and its results:
Square Square Root
I I
IV II
IX III
XVI IV
XXV V
XXXVI VI
XLIX VII
LXIV VIII
LXXXI IX
C X
CXXI XI
CXLIV XII
CLXIX XIII
CXCVI XIV
CCXXV XV
CCLVI XVI
CCLXXXIX XVII
CCCXXIV XVIII
CCCLXI XIX
CD XX
CDXLI XXI
CDLXXXIV XXII
DXXIX XXIII
DLXXVI XXIV
DCXXV XXV
DCLXXVI XXVI
DCCXXIX XXVII
DCCLXXXIV XXVIII
DCCCXLI XXIX
CM XXX
CMLXI XXXI
MXXIV XXXII
MLXXXIX XXXIII
MCLVI XXXIV
MCCXXV XXXV
MCCXCVI XXXVI
MCCCLXIX XXXVII
MCDXLIV XXXVIII
MDXXI XXXIX
MDC XL
MDCLXXXI XLI
MDCCLXIV XLII
MDCCCXLIX XLIII
MCMXXXVI XLIV
MMXXV XLV
MMCXVI XLVI
MMCCIX XLVII
MMCCCIV XLVIII
MMCDI XLIX
MMD L
MMDCI LI
MMDCCIV LII
MMDCCCIX LIII
MMCMXVI LIV
MMMXXV LV
MMMCXXXVI LVI
MMMCCXLIX LVII
MMMCCCLXIV LVIII
MMMCDLXXXI LIX
MMMDC LX
MMMDCCXXI LXI
MMMDCCCXLIV LXII
MMMCMLXIX LXIII
|
Posted by Charlie
on 2022-05-15 10:59:12 |