Retrieved de https://studentshare.org/information-technology/1579492-analyze-code-3
https://studentshare.org/information-technology/1579492-analyze-code-3.
Choose one algorithm that was created and posted by another to solve the same problem, and analyze the differences between the effectiveness of the two algorithms. Post the result of the analysis, and improvements.DQ1:CK,ML,JL,AL,KSem Code 1The following optimizations have been used:1) To verify if a number is divisible by three it is enough to make sure the sum of the numbers digits is divisible by three. A counter ModSoFar is kept to see what the mod of the sum of the digits is in the currently tested string.
This optimization helps contain the very large division that occurs on all of the tests to a mod of a number that is 11 at most. This might be optimized even further by changing the mod to an if and specifying if the ModSoFar is equal to 0,3,6 or 9 then.but that depends on the efficiency of the CPU and whether four comparisons is faster than one mod. Similar shortcuts are available for all of the numbers under 10 (Magness, Sinnot & Ward, 2004)2) In order to save going over the Digits to find the sub string that is needed for printing, the program keeps the growing string in a variable adding and additional characters as needed.
As a side note, according to Brookshear (2007) the indentation rules require an else to be directly under the indented “then” of an “if” which is confusing when there is an “if” with no “else” inside an “if” with one (see lines 16 and 19 where the “else” actually belongs to the “if” on line 13).Procedure SubStrMod3(Digits) StartChar
Read More