├── README.md ├── MUMSPT ├── App.config ├── Properties │ └── AssemblyInfo.cs ├── TestSet10 │ ├── TestSet10.cs │ └── TestSet10Test.cs ├── TestSet31 │ ├── TestSet31.cs │ └── TestSet31Test.cs ├── TestSet7 │ ├── TestSet7Test.cs │ └── TestSet7.cs ├── TestSet14 │ ├── TestSet14.cs │ └── TestSet14Test.cs ├── TestSet6 │ ├── TestSet6Test.cs │ └── TestSet6.cs ├── TestSet22 │ ├── TestSet22Test.cs │ └── TestSet22.cs ├── TestSet2 │ ├── TestSet2Test.cs │ └── TestSet2.cs ├── TestSet5 │ ├── TestSet5Test.cs │ └── TestSet5.cs ├── TestSet28 │ ├── TestSet28.cs │ └── TestSet28Test.cs ├── TestSet23 │ ├── TestSet23Test.cs │ └── TestSet23.cs ├── TestSet27 │ ├── TestSet27Test.cs │ └── TestSet27.cs ├── Helper.cs ├── TestSet11 │ ├── TestSet11Test.cs │ └── TestSet11.cs ├── TestSet3 │ ├── TestSet3Test.cs │ └── TestSet3.cs ├── TestSet20 │ └── TestSet20.cs ├── TestSet21 │ ├── TestSet21Test.cs │ └── TestSet21.cs ├── TestSet4 │ ├── TestSet4Test.cs │ └── TestSet4.cs ├── TestSet19 │ ├── TestSet19Test.cs │ └── TestSet19.cs ├── TestSet16 │ ├── TestSet16.cs │ └── TestSet16Test.cs ├── TestSet24 │ ├── TestSet24Test.cs │ └── TestSet24.cs ├── TestSet8 │ ├── TestSet8Test.cs │ └── TestSet8.cs ├── TestSet30 │ └── TestSet30.cs ├── TestSet13 │ └── TestSet13Test.cs ├── TestSet1 │ └── TestSet1Test.cs ├── TestSet26 │ └── TestSet26Test.cs ├── TestSet9 │ ├── TestSet9.cs │ └── TestSet9Test.cs ├── TestSet17 │ └── TestSet17.cs ├── TestSet15 │ └── TestSet15.cs ├── TestSet25 │ └── TestSet25.cs └── MUMSPT.csproj ├── MUMSPT.sln ├── .gitattributes └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # MUMSPT 2 | M.U.M Programing Test 3 | -------------------------------------------------------------------------------- /MUMSPT/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /MUMSPT.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MUMSPT", "MUMSPT\MUMSPT.csproj", "{57EC70D8-C4BE-4CED-BC70-54347C4ACE4B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {57EC70D8-C4BE-4CED-BC70-54347C4ACE4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {57EC70D8-C4BE-4CED-BC70-54347C4ACE4B}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {57EC70D8-C4BE-4CED-BC70-54347C4ACE4B}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {57EC70D8-C4BE-4CED-BC70-54347C4ACE4B}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /MUMSPT/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MUMSPT")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MUMSPT")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("57ec70d8-c4be-4ced-bc70-54347c4ace4b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /MUMSPT/TestSet10/TestSet10.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet10 8 | { 9 | public static class TestSet10 10 | { 11 | /// 12 | /// Write a function named sameNumberOfFactors that takes two integer 13 | /// arguments and returns 1 if they have the same number of factors. 14 | /// If either argument is negative, return -1. Otherwise return 0. 15 | /// 16 | /// 17 | /// 18 | /// 19 | public static int sameNumberOfFactors(int n1, int n2) 20 | { 21 | int isSame = 0; 22 | 23 | // check for negative numbers 24 | if (n1 < 0 || n2 < 0) isSame = -1; 25 | 26 | // check if the same number 27 | if (n1 == n2) isSame = 1; 28 | 29 | if (isSame == 0) 30 | { 31 | int n1count = 0, n2count = 0; 32 | // get the factors of n1 33 | 34 | for (int i = 1; i <= n1; i++) 35 | { 36 | if (n1 % i == 0) n1count++; 37 | } 38 | 39 | for (int i = 1; i <= n2; i++) 40 | { 41 | if (n2 % i == 0) n2count++; 42 | } 43 | 44 | if (n1count == n2count) isSame = 1; 45 | } 46 | 47 | return isSame; 48 | } 49 | 50 | /// 51 | /// Solved in TestSet9 52 | /// 53 | /// 54 | /// 55 | /// 56 | public static int equivalentArrays(int[] a1, int[] a2) 57 | { 58 | return TestSet9.TestSet9.equivalentArrays(a1, a2); 59 | } 60 | 61 | public static int hasSingleMaximum(int[] a) 62 | { 63 | int hasSingle = 0; 64 | int maxcount = 0, max = 0; 65 | for (int i = 0; i < a.Length; i++) 66 | { 67 | if(a[i] > max) { 68 | max = a[i]; 69 | maxcount = 1; 70 | }else if(a[i] == max) 71 | { 72 | maxcount++; 73 | } 74 | } 75 | if (maxcount == 1) hasSingle = 1; 76 | 77 | return hasSingle; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /MUMSPT/TestSet31/TestSet31.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet31 8 | { 9 | public static class TestSet31 10 | { 11 | public static int isDual(int[] a) 12 | { 13 | int isDual = 1; 14 | 15 | if (a.Length % 2 != 0) isDual = 0; 16 | 17 | int sum = 0; 18 | 19 | for (int i = 0; i < a.Length && isDual == 1; i = i + 2) 20 | { 21 | if (i == 0) 22 | { 23 | sum = a[i] + a[i + 1]; 24 | } 25 | else 26 | { 27 | if (sum != a[i] + a[i + 1]) 28 | { 29 | isDual = 0; 30 | } 31 | } 32 | } 33 | 34 | return isDual; 35 | } 36 | 37 | public static int isAllPossibilities(int[] a) 38 | { 39 | int isAllPosibilities = 1; 40 | 41 | if (a.Length == 0) isAllPosibilities = 0; 42 | 43 | for (int i = 0; i < a.Length && isAllPosibilities == 1; i++) 44 | { 45 | int index = -1; 46 | for (int j = 0; j < a.Length && index == -1; j++) 47 | { 48 | if (i == a[j]) index = j; 49 | } 50 | if (index == -1) 51 | isAllPosibilities = 0; 52 | } 53 | 54 | return isAllPosibilities; 55 | } 56 | 57 | 58 | public static int isLayered(int[] a) 59 | { 60 | int isLayered = 1; 61 | if (a.Length == 0) isLayered = 0; 62 | int count = 0; 63 | for (int i = 0; i < a.Length && isLayered == 1; i++) 64 | { 65 | count++; 66 | if (i < a.Length - 1) 67 | { 68 | if (a[i] != a[i + 1]) 69 | { 70 | if (count < 2) 71 | isLayered = 0; 72 | count = 0; 73 | } 74 | if (a[i] > a[i + 1]) 75 | { 76 | isLayered = 0; 77 | } 78 | } 79 | else 80 | { 81 | if (count < 2) 82 | isLayered = 0; 83 | } 84 | } 85 | 86 | return isLayered; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /MUMSPT/TestSet7/TestSet7Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet7 8 | { 9 | public static class TestSet7Test 10 | { 11 | public static void matches() 12 | { 13 | Console.WriteLine("test for matches "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("array { 1, 2, 3, -5, -5, 2, 3, 18 }, { 3, -2, 3} is"); Console.WriteLine(" = {0}" 17 | , TestSet7.matches(new int[] { 1, 2, 3, -5, -5, 2, 3, 18 }, new int[] { 3, -2, 3})); 18 | 19 | Console.Write("array {1, 2, 3, -5, -5, 2, 3, 18}, {4, -1, 3} is"); Console.WriteLine(" = {0}" 20 | , TestSet7.matches(new int[] { 1, 2, 3, -5, -5, 2, 3, 18 }, new int[] { 4, -1, 3 })); 21 | 22 | 23 | Console.WriteLine("========================"); 24 | } 25 | public static void isStacked() 26 | { 27 | Console.WriteLine("test for isStacked "); 28 | Console.WriteLine("========================"); 29 | 30 | Console.Write("isStacked of 1 is"); Console.WriteLine(" = {0}" 31 | , TestSet7.isStacked(1)); 32 | 33 | Console.Write("isStacked of 3 is"); Console.WriteLine(" = {0}" 34 | , TestSet7.isStacked(3)); 35 | 36 | Console.Write("isStacked of 6 is"); Console.WriteLine(" = {0}" 37 | , TestSet7.isStacked(6)); 38 | Console.Write("isStacked of 7 is"); Console.WriteLine(" = {0}" 39 | , TestSet7.isStacked(7)); 40 | Console.Write("isStacked of 8 is"); Console.WriteLine(" = {0}" 41 | , TestSet7.isStacked(8)); 42 | 43 | Console.Write("isStacked of 9 is"); Console.WriteLine(" = {0}" 44 | , TestSet7.isStacked(9)); 45 | Console.Write("isStacked of 10 is"); Console.WriteLine(" = {0}" 46 | , TestSet7.isStacked(10)); 47 | 48 | Console.Write("isStacked of 15 is"); Console.WriteLine(" = {0}" 49 | , TestSet7.isStacked(15)); 50 | 51 | Console.WriteLine("========================"); 52 | } 53 | 54 | public static void isSumSafe() 55 | { 56 | Console.WriteLine("test for isSumSafe "); 57 | Console.WriteLine("========================"); 58 | 59 | Console.Write("isSumSafe of {5, -5, 0} is"); Console.WriteLine(" = {0}" 60 | , TestSet7.isSumSafe(new int[] { 5, -5, 0 })); 61 | 62 | Console.Write("isSumSafe of {5, -2, 1} is"); Console.WriteLine(" = {0}" 63 | , TestSet7.isSumSafe(new int[] { 5, -2, 1 })); 64 | 65 | Console.WriteLine("========================"); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /MUMSPT/TestSet10/TestSet10Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet10 8 | { 9 | public static class TestSet10Test 10 | { 11 | public static void sameNumberOfFactors() 12 | { 13 | Console.WriteLine("test for sameNumberOfFactors "); 14 | Console.WriteLine("========================"); 15 | Console.Write("sameNumberOfFactors of -6, 21 is"); Console.WriteLine(" = {0}" 16 | , TestSet10.sameNumberOfFactors(-6, 21)); 17 | 18 | Console.Write("sameNumberOfFactors of 6, 21 is"); Console.WriteLine(" = {0}" 19 | , TestSet10.sameNumberOfFactors(6, 21)); 20 | 21 | Console.Write("sameNumberOfFactors of 8, 12 is"); Console.WriteLine(" = {0}" 22 | , TestSet10.sameNumberOfFactors(8, 12)); 23 | 24 | 25 | Console.Write("sameNumberOfFactors of 23, 97 is"); Console.WriteLine(" = {0}" 26 | , TestSet10.sameNumberOfFactors(23, 97)); 27 | 28 | 29 | Console.Write("sameNumberOfFactors of 0, 1 is"); Console.WriteLine(" = {0}" 30 | , TestSet10.sameNumberOfFactors(0, 1)); 31 | 32 | Console.Write("sameNumberOfFactors of 0, 0 is"); Console.WriteLine(" = {0}" 33 | , TestSet10.sameNumberOfFactors(0, 0)); 34 | Console.WriteLine("========================"); 35 | } 36 | 37 | public static void hasSingleMaximum() 38 | { 39 | Console.WriteLine("test for hasSingleMaximum "); 40 | Console.WriteLine("========================"); 41 | 42 | Console.Write("hasSingleMaximum of {1, 2, 3, 1, 0} is"); Console.WriteLine(" = {0}" 43 | , TestSet10.hasSingleMaximum(new int[] { 1, 2, 3, 1, 0 })); 44 | 45 | Console.Write("hasSingleMaximum of {18} is"); Console.WriteLine(" = {0}" 46 | , TestSet10.hasSingleMaximum(new int[] { 18 })); 47 | 48 | Console.Write("hasSingleMaximum of {1, 2, 3, 0, 1, 3} is"); Console.WriteLine(" = {0}" 49 | , TestSet10.hasSingleMaximum(new int[] { 1, 2, 3, 0, 1, 3 })); 50 | 51 | Console.Write("hasSingleMaximum of {13, 1, 13, 2, 13, 0, 13, 1, 13} is"); Console.WriteLine(" = {0}" 52 | , TestSet10.hasSingleMaximum(new int[] { 13, 1, 13, 2, 13, 0, 13, 1, 13 })); 53 | 54 | Console.Write("hasSingleMaximum of { } is"); Console.WriteLine(" = {0}" 55 | , TestSet10.hasSingleMaximum(new int[] { })); 56 | 57 | Console.Write("hasSingleMaximum of {-6, -6, -6, -6, -6, -6, -6} is"); Console.WriteLine(" = {0}" 58 | , TestSet10.hasSingleMaximum(new int[] { -6, -6, -6, -6, -6, -6, -6 })); 59 | 60 | Console.WriteLine("========================"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /MUMSPT/TestSet14/TestSet14.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MUMSPT.TestSet14 4 | { 5 | public static class TestSet14 6 | { 7 | public static int fullnessQuotient(int n) 8 | { 9 | int isfullness = 0; 10 | 11 | if (n < 0) return -1; 12 | 13 | for (int i = 2; i < 10; i++) 14 | { 15 | // conver the number to bases 16 | int num = n; 17 | int j = 0; 18 | int hasezero = 0; 19 | int sum = 0; 20 | do 21 | { 22 | int med = num % i; 23 | sum += med * (int)Math.Pow(i, j); 24 | if (med == 0) 25 | { 26 | hasezero = 1; 27 | } 28 | num = num / i; 29 | j++; 30 | } 31 | while (sum < n); 32 | if (hasezero == 0) 33 | isfullness++; 34 | } 35 | 36 | return isfullness; 37 | } 38 | 39 | public static int isPacked(int[] a) 40 | { 41 | int isPacked = 1; 42 | int allpositive = 1; 43 | //int indexcount = 0; 44 | for (int i = 0; i < a.Length && isPacked == 1; i++) 45 | { 46 | int count = 0; 47 | //bool exitloop = false; 48 | for (int j = 0; j < a.Length && isPacked == 1; j++) 49 | { 50 | if (a[i] < 0) allpositive = 0; 51 | 52 | if (a[i] == a[j]) 53 | { 54 | count++; 55 | } 56 | else 57 | { 58 | if (j >= i) 59 | if (count != a[i]) isPacked = 0; 60 | } 61 | } 62 | if (count != a[i]) isPacked = 0; 63 | } 64 | if (allpositive == 0) 65 | isPacked = allpositive; 66 | return isPacked; 67 | } 68 | 69 | public static int isOddHeavy(int[] a) 70 | { 71 | int isOddHeavy = 1; 72 | 73 | int oddcount = 0; 74 | for (int i = 0; i < a.Length && isOddHeavy ==1; i++) 75 | { 76 | if(a[i]% 2 == 1) { 77 | oddcount++; 78 | for (int j = 0; j < a.Length && isOddHeavy ==1; j++) 79 | { 80 | if(a[j] % 2 == 0) { 81 | if (a[i] < a[j]) isOddHeavy = 0; 82 | } 83 | } 84 | } 85 | 86 | } 87 | 88 | // check for oddcount 89 | if (oddcount < 1) isOddHeavy = 0; 90 | return isOddHeavy; 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /MUMSPT/TestSet6/TestSet6Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet6 8 | { 9 | public static class TestSet6Test 10 | { 11 | public static void isSquare() 12 | { 13 | Console.WriteLine("test for isSquare "); 14 | Console.WriteLine("========================"); 15 | 16 | 17 | Console.Write("isSquare of 4 is"); Console.WriteLine(" = {0}" 18 | , TestSet6.isSquare(4)); 19 | 20 | Console.Write("isSquare of 25 is"); Console.WriteLine(" = {0}" 21 | , TestSet6.isSquare(25)); 22 | 23 | 24 | Console.Write("isSquare of -4 is"); Console.WriteLine(" = {0}" 25 | , TestSet6.isSquare(-4)); 26 | 27 | 28 | Console.Write("isSquare of 8 is"); Console.WriteLine(" = {0}" 29 | , TestSet6.isSquare(8)); 30 | 31 | 32 | Console.Write("isSquare of 0 is"); Console.WriteLine(" = {0}" 33 | , TestSet6.isSquare(0)); 34 | 35 | 36 | Console.WriteLine("========================"); 37 | } 38 | 39 | public static void isLegalNumber() 40 | { 41 | Console.WriteLine("test for isLegalNumber "); 42 | Console.WriteLine("========================"); 43 | 44 | Console.Write("array { 3, 2, 1 } and 4 is"); Console.WriteLine(" = {0}" 45 | , TestSet6.isLegalNumber(new int[] { 3, 2, 1 }, 4)); 46 | 47 | Console.Write("array {3, 7, 1} and 6 is"); Console.WriteLine(" = {0}" 48 | , TestSet6.isLegalNumber(new int[] { 3, 7, 1 }, 6)); 49 | 50 | 51 | 52 | Console.WriteLine("========================"); 53 | } 54 | 55 | public static void convertToBase10() 56 | { 57 | Console.WriteLine("test for convertToBase10 "); 58 | Console.WriteLine("========================"); 59 | 60 | Console.Write("array {1, 0, 1, 1} , 2 is"); Console.WriteLine(" = {0}" 61 | , TestSet6.convertToBase10(new int[] { 1, 0, 1, 1 }, 2)); 62 | 63 | Console.Write("array {1, 1, 2}, 3 is"); Console.WriteLine(" = {0}" 64 | , TestSet6.convertToBase10(new int[] { 1, 1, 2 }, 3)); 65 | 66 | Console.Write("array {3, 2, 5}, 8 is"); Console.WriteLine(" = {0}" 67 | , TestSet6.convertToBase10(new int[] { 3, 2, 5 }, 8)); 68 | 69 | Console.Write("array {3, 7, 1}, 6 is"); Console.WriteLine(" = {0}" 70 | , TestSet6.convertToBase10(new int[] { 3, 7, 1 }, 6)); 71 | 72 | Console.Write("array {3, 2, 1}, 4 is"); Console.WriteLine(" = {0}" 73 | , TestSet6.convertToBase10(new int[] { 3, 2, 1 }, 4)); 74 | 75 | Console.WriteLine("========================"); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /MUMSPT/TestSet22/TestSet22Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet22 8 | { 9 | public static class TestSet22Test 10 | { 11 | public static void isHodder() 12 | { 13 | Console.WriteLine("test for isHodder "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isHodder of 3 is"); Console.WriteLine(" = {0}" 17 | , TestSet22.isHodder(3)); 18 | 19 | Console.Write("isHodder of 7 is"); Console.WriteLine(" = {0}" 20 | , TestSet22.isHodder(7)); 21 | 22 | 23 | Console.Write("isHodder of 31 is"); Console.WriteLine(" = {0}" 24 | , TestSet22.isHodder(31)); 25 | 26 | Console.Write("isHodder of 127 is"); Console.WriteLine(" = {0}" 27 | , TestSet22.isHodder(127)); 28 | 29 | 30 | Console.WriteLine("========================"); 31 | } 32 | 33 | public static void areAnagrams() 34 | { 35 | Console.WriteLine("test for areAnagrams "); 36 | Console.WriteLine("========================"); 37 | 38 | Console.Write("areAnagrams of {'s', 'i', 't'},{'i', 't', 's'} is"); Console.WriteLine(" = {0}" 39 | , TestSet22.areAnagrams(new char[] {'s', 'i', 't'}, new char[] {'i', 't', 's'})); 40 | 41 | Console.Write("areAnagrams of {'s', 'i', 't'} and {'i', 'd', 's'} is"); Console.WriteLine(" = {0}" 42 | , TestSet22.areAnagrams(new char[] { 's', 'i', 't' }, new char[] { 'i', 'd', 's' })); 43 | 44 | Console.Write("areAnagrams of {'b', 'i', 'g'} and {'b', 'i', 't'} is"); Console.WriteLine(" = {0}" 45 | , TestSet22.areAnagrams(new char[] { 'b', 'i', 'g' }, new char[] { 'b', 'i', 't' })); 46 | 47 | Console.Write("areAnagrams of {'b', 'o', 'g'} and {'b', 'o', 'o'} is"); Console.WriteLine(" = {0}" 48 | , TestSet22.areAnagrams(new char[] { 'b', 'o', 'g' }, new char[] { 'b', 'o', 'o' })); 49 | 50 | Console.Write("areAnagrams of {} and {} is"); Console.WriteLine(" = {0}" 51 | , TestSet22.areAnagrams(new char[] { }, new char[] { })); 52 | 53 | Console.Write("areAnagrams of {'b', 'i', 'g'} and {'b', 'i', 'g'} is"); Console.WriteLine(" = {0}" 54 | , TestSet22.areAnagrams(new char[] { 'b', 'i', 'g' }, new char[] { 'b', 'i', 'g' })); 55 | 56 | Console.Write("areAnagrams of {'p', 'o', 'o', 'l'} and {'p', 'o', 'l', 'l'} is"); Console.WriteLine(" = {0}" 57 | , TestSet22.areAnagrams(new char[] { 'p', 'o', 'o', 'l' }, new char[] { 'p', 'o', 'l', 'l' })); 58 | 59 | Console.WriteLine("========================"); 60 | } 61 | 62 | public static void closestFibonacci() 63 | { 64 | TestSet11.TestSet11Test.closestFibonacci(); 65 | 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /MUMSPT/TestSet2/TestSet2Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet2 8 | { 9 | public static class TestSet2Test 10 | { 11 | public static void countSquarePairs() 12 | { 13 | Console.WriteLine("test for countSquarePairs "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("array {11, 5, 4, 20} is"); Console.WriteLine(" = {0}" 17 | , TestSet2.countSquarePairs(new int[] { 11, 5, 4, 20 })); 18 | 19 | Console.Write("array {9, 0, 2, -5, 7} is"); Console.WriteLine(" = {0}" 20 | , TestSet2.countSquarePairs(new int[] { 9, 0, 2, -5, 7 })); 21 | 22 | Console.Write("array {9} is"); Console.WriteLine(" = {0}" 23 | , TestSet2.countSquarePairs(new int[] { 9 })); 24 | 25 | Console.WriteLine("========================"); 26 | 27 | } 28 | 29 | public static void findPorcupineNumber() 30 | { 31 | Console.WriteLine("test for findPorcupineNumber "); 32 | Console.WriteLine("========================"); 33 | 34 | Console.Write("PorcupineNumber for 0 is"); Console.WriteLine(" = {0}" 35 | , TestSet2.findPorcupineNumber(0)); 36 | 37 | Console.Write("PorcupineNumber for 138 is"); Console.WriteLine(" = {0}" 38 | , TestSet2.findPorcupineNumber(138)); 39 | 40 | Console.Write("PorcupineNumber for 139 is"); Console.WriteLine(" = {0}" 41 | , TestSet2.findPorcupineNumber(139)); 42 | 43 | Console.WriteLine("========================"); 44 | 45 | } 46 | 47 | public static void isGuthrieSequence() 48 | { 49 | Console.WriteLine("test for isGuthrieSequence "); 50 | Console.WriteLine("========================"); 51 | 52 | Console.Write("PorcupineNumber for { 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 } is"); Console.WriteLine(" = {0}" 53 | , TestSet2.isGuthrieSequence(new int[] { 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 })); 54 | 55 | Console.Write("PorcupineNumber for {8, 4, 2, 1} is"); Console.WriteLine(" = {0}" 56 | , TestSet2.isGuthrieSequence(new int[] { 8, 4, 2, 1 })); 57 | 58 | Console.Write("PorcupineNumber for {8, 17, 4, 1} is"); Console.WriteLine(" = {0}" 59 | , TestSet2.isGuthrieSequence(new int[] { 8, 17, 4, 1 })); 60 | 61 | Console.Write("PorcupineNumber for {8, 4, 1} is"); Console.WriteLine(" = {0}" 62 | , TestSet2.isGuthrieSequence(new int[] { 8, 4, 1 })); 63 | 64 | Console.Write("PorcupineNumber for {8, 4, 2} is"); Console.WriteLine(" = {0}" 65 | , TestSet2.isGuthrieSequence(new int[] { 8, 4, 2 })); 66 | 67 | Console.WriteLine("========================"); 68 | 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /MUMSPT/TestSet5/TestSet5Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet5 8 | { 9 | public static class TestSet5Test 10 | { 11 | public static void henry() 12 | { 13 | Console.WriteLine("test for henry "); 14 | Console.WriteLine("========================"); 15 | 16 | 17 | Console.Write("henry of 1,3 is"); Console.WriteLine(" = {0}" 18 | , TestSet5.henry(1, 3)); 19 | 20 | 21 | Console.WriteLine("========================"); 22 | } 23 | 24 | public static void isDivisible() 25 | { 26 | Console.WriteLine("test for isDivisible "); 27 | Console.WriteLine("========================"); 28 | 29 | Console.Write("array {3, 3, 6, 36} and 3 is"); Console.WriteLine(" = {0}" 30 | , TestSet5.isDivisible(new int[] { 3, 3, 6, 36 }, 3)); 31 | 32 | Console.Write("array {4} and 2 is"); Console.WriteLine(" = {0}" 33 | , TestSet5.isDivisible(new int[] { 4 }, 2)); 34 | 35 | Console.Write("array {3, 4, 3, 6, 36} and 3 is"); Console.WriteLine(" = {0}" 36 | , TestSet5.isDivisible(new int[] { 3, 4, 3, 6, 36 }, 3)); 37 | 38 | Console.Write("array {6, 12, 24, 36} and 12 is"); Console.WriteLine(" = {0}" 39 | , TestSet5.isDivisible(new int[] { 6, 12, 24, 36 }, 12)); 40 | 41 | Console.Write("array { } and 12 is"); Console.WriteLine(" = {0}" 42 | , TestSet5.isDivisible(new int[] { }, 12)); 43 | 44 | Console.WriteLine("========================"); 45 | } 46 | 47 | public static void isNUnique() 48 | { 49 | Console.WriteLine("test for isNUnique "); 50 | Console.WriteLine("========================"); 51 | 52 | Console.Write("array {7, 3, 3, 2, 4} and 6 is"); Console.WriteLine(" = {0}" 53 | , TestSet5.isNUnique(new int[] { 7, 3, 3, 2, 4 }, 6)); 54 | 55 | Console.Write("array {7, 3, 3, 2, 4} and 10 is"); Console.WriteLine(" = {0}" 56 | , TestSet5.isNUnique(new int[] { 7, 3, 3, 2, 4 }, 10)); 57 | 58 | Console.Write("array {7, 3, 3, 2, 4} and 11 is"); Console.WriteLine(" = {0}" 59 | , TestSet5.isNUnique(new int[] { 7, 3, 3, 2, 4 }, 11)); 60 | 61 | Console.Write("array {7, 3, 3, 2, 4} and 8 is"); Console.WriteLine(" = {0}" 62 | , TestSet5.isNUnique(new int[] { 7, 3, 3, 2, 4 }, 8)); 63 | 64 | Console.Write("array {7, 3, 3, 2, 4} and 4 is"); Console.WriteLine(" = {0}" 65 | , TestSet5.isNUnique(new int[] { 7, 3, 3, 2, 4 }, 4)); 66 | 67 | Console.Write("array { 1 } and 12 is"); Console.WriteLine(" = {0}" 68 | , TestSet5.isNUnique(new int[] { 1 }, 12)); 69 | 70 | Console.WriteLine("========================"); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /MUMSPT/TestSet28/TestSet28.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet28 8 | { 9 | public static class TestSet28 10 | { 11 | 12 | /// 13 | /// Write a function named allValuesTheSame that 14 | /// returns 1 if all elements of its argument array 15 | /// have the same value. Otherwise, it returns 0. 16 | /// 17 | /// 18 | /// 19 | public static int allValuesTheSame(int[] a) 20 | { 21 | int isAllValues = 1; 22 | // if empty array set to 0 and don't enter the loop 23 | if (a.Length == 0) 24 | isAllValues = 0; 25 | 26 | int first = 0, current = 0; 27 | for (int i = 0; i < a.Length && isAllValues == 1; i++) 28 | { 29 | if (i == 0) 30 | first = a[i]; 31 | current = a[i]; 32 | if (first != current) isAllValues = 0; 33 | } 34 | return isAllValues; 35 | } 36 | 37 | 38 | /// 39 | /// Write a function named hasNValues which takes an 40 | /// array and an integer n as arguments. 41 | /// It returns true if all the elements 42 | /// of the array are one of n different values 43 | /// 44 | /// 45 | /// 46 | /// 47 | public static int hasNValues(int[] a, int n) 48 | { 49 | int hasNValues = 1; 50 | int[] uniqueValues = new int[n]; 51 | int uniqueindex = 0; 52 | for (int i = 0; i < a.Length && hasNValues == 1; i++) 53 | { 54 | if (Helper.isExistInArray(uniqueValues, a[i]) == -1) 55 | { 56 | if (uniqueindex < n) 57 | { 58 | uniqueValues[uniqueindex] = a[i]; 59 | uniqueindex++; 60 | } 61 | else 62 | { 63 | hasNValues = 0; 64 | } 65 | } 66 | } 67 | 68 | if (uniqueindex != n) hasNValues = 0; 69 | 70 | return hasNValues; 71 | } 72 | 73 | /// 74 | /// Write a function named sameNumberOfFactors that takes two integer 75 | /// arguments and returns 1 if they have the same number of factors. 76 | /// If either argument is negative, return -1. Otherwise return 0. 77 | /// 78 | /// 79 | /// 80 | /// 81 | public static int sameNumberOfFactors(int n1, int n2) 82 | { 83 | return TestSet10.TestSet10.sameNumberOfFactors(n1, n2); 84 | } 85 | 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /MUMSPT/TestSet6/TestSet6.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet6 8 | { 9 | public static class TestSet6 10 | { 11 | 12 | /// 13 | /// Write a function named isSquare that returns 1 14 | /// if its integer argument is a square of some integer, otherwise it returns 0. 15 | /// 16 | /// 17 | /// 18 | public static int isSquare(int n) 19 | { 20 | int isSquare = 0; 21 | 22 | int index = 0, sum = 0; 23 | do 24 | { 25 | sum = index * index; 26 | if (n == sum) 27 | isSquare = 1; 28 | 29 | index++; 30 | } while (sum <= n && isSquare == 0); 31 | 32 | return isSquare; 33 | } 34 | 35 | 36 | /// 37 | /// Write a method named isLegalNumber that takes two arguments. 38 | /// The first argument is an array whose elements are the digits of the number to test. 39 | /// The second argument is the base of the number represented by the first argument. 40 | /// The method returns 1 if the number represented by the array is a legal number in 41 | /// the given base, otherwise it returns 0. 42 | /// However, since all digits of a base n number must be less than n, 43 | /// the following call will return 0 because 3716 is not a legal 44 | /// base 6 number (the digit 7 is not allowed) 45 | /// 46 | /// 47 | /// 48 | /// 49 | public static int isLegalNumber(int[] a, int bases) 50 | { 51 | int isLegalNumber = 1; 52 | 53 | for (int i = 0; i < a.Length && isLegalNumber == 1; i++) 54 | { 55 | if (a[i] >= bases) isLegalNumber = 0; 56 | 57 | } 58 | return isLegalNumber; 59 | } 60 | 61 | 62 | 63 | 64 | /// 65 | /// Using the representation for a number described in the 66 | /// second question write a method named convertToBase10 that 67 | /// converts its "array, base" arguments to a base 10 number if 68 | /// the input is legal for the specified base. If it is not, it returns -1. 69 | /// 70 | /// 71 | /// 72 | /// 73 | public static int convertToBase10(int[] a,int bases) 74 | { 75 | int base10 = 0,islegal = isLegalNumber(a, bases); 76 | int exp = 0; 77 | for (int i = a.Length - 1; i >= 0 && islegal == 1; i--, exp++) 78 | { 79 | base10 += a[i] * (int)Math.Pow(bases, exp); 80 | 81 | } 82 | 83 | return base10; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /MUMSPT/TestSet23/TestSet23Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet23 8 | { 9 | public static class TestSet23Test 10 | { 11 | public static void vesuvian() 12 | { 13 | Console.WriteLine("test for vesuvian "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("vesuvian of 50 is"); Console.WriteLine(" = {0}" 17 | , TestSet23.vesuvian(50)); 18 | 19 | Console.Write("vesuvian of 51 is"); Console.WriteLine(" = {0}" 20 | , TestSet23.vesuvian(51)); 21 | 22 | Console.Write("vesuvian of 65 is"); Console.WriteLine(" = {0}" 23 | , TestSet23.vesuvian(65)); 24 | 25 | Console.Write("vesuvian of 85 is"); Console.WriteLine(" = {0}" 26 | , TestSet23.vesuvian(85)); 27 | 28 | 29 | Console.WriteLine("========================"); 30 | 31 | } 32 | 33 | public static void isOneBalanced() 34 | { 35 | Console.WriteLine("test for isOneBalanced "); 36 | Console.WriteLine("========================"); 37 | 38 | Console.Write("isOneBalanced of {1, 1, 1, 2, 3, -18, 45, 1} is"); Console.WriteLine(" = {0}" 39 | , TestSet23.isOneBalanced(new int[] { 1, 1, 1, 2, 3, -18, 45, 1 })); 40 | 41 | Console.Write("isOneBalanced of {1, 1, 1, 2, 3, -18, 45, 1, 0} is"); Console.WriteLine(" = {0}" 42 | , TestSet23.isOneBalanced(new int[] { 1, 1, 1, 2, 3, -18, 45, 1, 0 })); 43 | 44 | Console.Write("isOneBalanced of {1, 1, 2, 3, 1, -18, 26, 1} is"); Console.WriteLine(" = {0}" 45 | , TestSet23.isOneBalanced(new int[] { 1, 1, 2, 3, 1, -18, 26, 1 })); 46 | 47 | Console.Write("isOneBalanced of {} is"); Console.WriteLine(" = {0}" 48 | , TestSet23.isOneBalanced(new int[] { })); 49 | 50 | Console.Write("isOneBalanced of {3, 4, 1, 1} is"); Console.WriteLine(" = {0}" 51 | , TestSet23.isOneBalanced(new int[] { 3, 4, 1, 1 })); 52 | 53 | Console.Write("isOneBalanced of {1, 1, 3, 4} is"); Console.WriteLine(" = {0}" 54 | , TestSet23.isOneBalanced(new int[] { 1, 1, 3, 4 })); 55 | 56 | Console.Write("isOneBalanced of {3, 3, 3, 3, 3, 3} is"); Console.WriteLine(" = {0}" 57 | , TestSet23.isOneBalanced(new int[] { 3, 3, 3, 3, 3, 3 })); 58 | 59 | Console.Write("isOneBalanced of {1, 1, 1, 1, 1, 1} is"); Console.WriteLine(" = {0}" 60 | , TestSet23.isOneBalanced(new int[] { 1, 1, 1, 1, 1, 1 })); 61 | 62 | 63 | Console.WriteLine("========================"); 64 | 65 | } 66 | 67 | public static void isFibonacci() 68 | { 69 | Console.WriteLine("test for isFibonacci "); 70 | Console.WriteLine("========================"); 71 | 72 | Console.Write("isFibonacci of 13 is"); Console.WriteLine(" = {0}" 73 | , TestSet23.isFibonacci(13)); 74 | 75 | 76 | Console.Write("isFibonacci of 27 is"); Console.WriteLine(" = {0}" 77 | , TestSet23.isFibonacci(27)); 78 | 79 | Console.WriteLine("========================"); 80 | 81 | } 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /MUMSPT/TestSet27/TestSet27Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet27 8 | { 9 | public static class TestSet27Test 10 | { 11 | public static void isSquare() 12 | { 13 | Console.WriteLine("test for isSquare "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isSquare of 4 is"); Console.WriteLine(" = {0}" 17 | , TestSet27.isSquare(4)); 18 | 19 | Console.Write("isSquare of 25 is"); Console.WriteLine(" = {0}" 20 | , TestSet27.isSquare(25)); 21 | 22 | 23 | Console.Write("isSquare of -4 is"); Console.WriteLine(" = {0}" 24 | , TestSet27.isSquare(-4)); 25 | 26 | Console.Write("isSquare of 8 is"); Console.WriteLine(" = {0}" 27 | , TestSet27.isSquare(8)); 28 | 29 | Console.Write("isSquare of 0 is"); Console.WriteLine(" = {0}" 30 | , TestSet27.isSquare(0)); 31 | 32 | Console.WriteLine("========================"); 33 | 34 | } 35 | 36 | public static void isComplete() 37 | { 38 | Console.WriteLine("test for isComplete "); 39 | Console.WriteLine("========================"); 40 | 41 | Console.Write("isComplete of {3, 2, 9, 5} is"); Console.WriteLine(" = {0}" 42 | , TestSet27.isComplete(new int[] { 3, 2, 9, 5 })); 43 | 44 | Console.Write("isComplete of {36, -28} is"); Console.WriteLine(" = {0}" 45 | , TestSet27.isComplete(new int[] { 36, -28 })); 46 | 47 | Console.Write("isComplete of {36, 28} is"); Console.WriteLine(" = {0}" 48 | , TestSet27.isComplete(new int[] { 36, 28 })); 49 | 50 | Console.Write("isComplete of {4} is"); Console.WriteLine(" = {0}" 51 | , TestSet27.isComplete(new int[] { 4 })); 52 | 53 | Console.Write("isComplete of {3, 2, 1, 1, 5, 6} is"); Console.WriteLine(" = {0}" 54 | , TestSet27.isComplete(new int[] { 3, 2, 1, 1, 5, 6 })); 55 | 56 | Console.Write("isComplete of {3, 7, 23, 13, 107, -99, 97, 81} is"); Console.WriteLine(" = {0}" 57 | , TestSet27.isComplete(new int[] { 3, 7, 23, 13, 107, -99, 97, 81 })); 58 | 59 | Console.WriteLine("========================"); 60 | 61 | } 62 | 63 | public static void loopSum() 64 | { 65 | Console.WriteLine("test for loopSum "); 66 | Console.WriteLine("========================"); 67 | 68 | Console.Write("loopSum of {1, 2, 3},2 is"); Console.WriteLine(" = {0}" 69 | , TestSet27.loopSum(new int[] { 1, 2, 3 },2)); 70 | 71 | Console.Write("loopSum of {-1, 2, -1},7 is"); Console.WriteLine(" = {0}" 72 | , TestSet27.loopSum(new int[] { -1, 2, -1 }, 7)); 73 | 74 | Console.Write("loopSum of {1, 4, 5, 6},4 is"); Console.WriteLine(" = {0}" 75 | , TestSet27.loopSum(new int[] { 1, 4, 5, 6 }, 4)); 76 | 77 | Console.Write("loopSum of {3},10 is"); Console.WriteLine(" = {0}" 78 | , TestSet27.loopSum(new int[] { 3 }, 10)); 79 | 80 | Console.WriteLine("========================"); 81 | 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /MUMSPT/Helper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT 8 | { 9 | public static class Helper 10 | { 11 | /// 12 | /// calucate if the number is prime or not 13 | /// 14 | /// the number that is under investigation 15 | /// true if the number is prime ,false if not 16 | public static bool isPrime(int n) 17 | { 18 | if (n <= 1) return false; 19 | for (int i = 2; i <= n / 2; i++) 20 | { 21 | if (n % i == 0) return false; 22 | } 23 | return true; 24 | } 25 | 26 | 27 | /// 28 | /// search the array for a element is exist in array or not 29 | /// 30 | /// the array that is under investigation 31 | /// the element being search about it int the array 32 | /// return the index of the element in the array of not exist return -1 33 | public static int isExistInArray(int[] b, int element) 34 | { 35 | int isExist = -1; 36 | 37 | for (int i = 0; i < b.Length && isExist == -1; i++) 38 | { 39 | if (b[i] == element) 40 | isExist = i; 41 | } 42 | return isExist; 43 | } 44 | 45 | public static int isExistInArray(char[] b, char element) 46 | { 47 | int isExist = -1; 48 | 49 | for (int i = 0; i < b.Length && isExist == -1; i++) 50 | { 51 | if (b[i] == element) 52 | isExist = i; 53 | } 54 | return isExist; 55 | } 56 | 57 | 58 | /// 59 | /// A perfect square is an integer whose square root is also an integer, 60 | /// e.g. 4, 9, 16 are perfect squares but 3, 10 and 17 are not. 61 | /// 62 | /// the number under invistegation 63 | /// 64 | public static bool isPerfectSquare(int n) 65 | { 66 | double sqrt = Math.Sqrt(n); 67 | return (sqrt == Math.Floor(sqrt)); 68 | } 69 | 70 | /// 71 | /// get the factors of number 72 | /// 73 | /// 74 | /// 75 | public static List Factors(int number) 76 | { 77 | List factors = new List(); 78 | for (int i = 1; i <= number; i++) 79 | { 80 | if (number % i == 0) 81 | { 82 | factors.Add(i); 83 | } 84 | 85 | } 86 | return factors; 87 | } 88 | 89 | 90 | /// 91 | /// get if the number is Integer or not 92 | /// 93 | /// 94 | /// 95 | public static bool IsWholeNumber(double n) 96 | { 97 | return (n % 1 == 0); 98 | } 99 | 100 | public static bool IsdivisibleBy(int number,int by) 101 | { 102 | return (number % by == 0); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /MUMSPT/TestSet11/TestSet11Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet11 8 | { 9 | public static class TestSet11Test 10 | { 11 | public static void isMartian() 12 | { 13 | Console.WriteLine("test for isMartian "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isMartian of {1, 3} is"); Console.WriteLine(" = {0}" 17 | , TestSet11.isMartian(new int[] { 1, 3 })); 18 | 19 | Console.Write("isMartian of {1, 2, 1, 2, 1, 2, 1, 2, 1} is"); Console.WriteLine(" = {0}" 20 | , TestSet11.isMartian(new int[] { 1, 2, 1, 2, 1, 2, 1, 2, 1 })); 21 | 22 | 23 | Console.Write("isMartian of {1, 3, 2} is"); Console.WriteLine(" = {0}" 24 | , TestSet11.isMartian(new int[] { 1, 3, 2 })); 25 | 26 | Console.Write("isMartian of {1, 3, 3, 2, 1} is"); Console.WriteLine(" = {0}" 27 | , TestSet11.isMartian(new int[] { 1, 3, 3, 2, 1 })); 28 | 29 | Console.Write("isMartian of {1, 2, -18, -18, 1, 2} is"); Console.WriteLine(" = {0}" 30 | , TestSet11.isMartian(new int[] { 1, 2, -18, -18, 1, 2 })); 31 | 32 | Console.Write("isMartian of {} is"); Console.WriteLine(" = {0}" 33 | , TestSet11.isMartian(new int[] { })); 34 | 35 | Console.Write("isMartian of {1} is"); Console.WriteLine(" = {0}" 36 | , TestSet11.isMartian(new int[] { 1 })); 37 | 38 | Console.Write("isMartian of {2} is"); Console.WriteLine(" = {0}" 39 | , TestSet11.isMartian(new int[] { 2 })); 40 | 41 | 42 | Console.WriteLine("========================"); 43 | } 44 | 45 | public static void closestFibonacci() 46 | { 47 | Console.WriteLine("test for closestFibonacci "); 48 | Console.WriteLine("========================"); 49 | 50 | Console.Write("closestFibonacci of 12 is"); Console.WriteLine(" = {0}" 51 | , TestSet11.closestFibonacci(12)); 52 | 53 | Console.Write("closestFibonacci of 33 is"); Console.WriteLine(" = {0}" 54 | , TestSet11.closestFibonacci(33)); 55 | 56 | Console.Write("closestFibonacci of 34 is"); Console.WriteLine(" = {0}" 57 | , TestSet11.closestFibonacci(34)); 58 | 59 | Console.Write("closestFibonacci of 0 is"); Console.WriteLine(" = {0}" 60 | , TestSet11.closestFibonacci(0)); 61 | Console.WriteLine("========================"); 62 | } 63 | 64 | public static void isPrimeHappy() 65 | { 66 | Console.WriteLine("test for isPrimeHappy "); 67 | Console.WriteLine("========================"); 68 | 69 | Console.Write("isPrimeHappy of 5 is"); Console.WriteLine(" = {0}" 70 | , TestSet11.isPrimeHappy(5)); 71 | 72 | Console.Write("isPrimeHappy of 25 is"); Console.WriteLine(" = {0}" 73 | , TestSet11.isPrimeHappy(25)); 74 | 75 | Console.Write("isPrimeHappy of 32 is"); Console.WriteLine(" = {0}" 76 | , TestSet11.isPrimeHappy(32)); 77 | 78 | Console.Write("isPrimeHappy of 8 is"); Console.WriteLine(" = {0}" 79 | , TestSet11.isPrimeHappy(8)); 80 | 81 | Console.Write("isPrimeHappy of 2 is"); Console.WriteLine(" = {0}" 82 | , TestSet11.isPrimeHappy(2)); 83 | 84 | Console.WriteLine("========================"); 85 | } 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /MUMSPT/TestSet28/TestSet28Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet28 8 | { 9 | public static class TestSet28Test 10 | { 11 | public static void allValuesTheSame() 12 | { 13 | Console.WriteLine("test for allValuesTheSame "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("allValuesTheSame of {1, 1, 1, 1} is"); Console.WriteLine(" = {0}" 17 | , TestSet28.allValuesTheSame(new int[] { 1, 1, 1, 1 })); 18 | 19 | Console.Write("allValuesTheSame of {83, 83, 83} is"); Console.WriteLine(" = {0}" 20 | , TestSet28.allValuesTheSame(new int[] { 83, 83, 83 })); 21 | 22 | Console.Write("allValuesTheSame of {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} is"); Console.WriteLine(" = {0}" 23 | , TestSet28.allValuesTheSame(new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); 24 | 25 | Console.Write("allValuesTheSame of {1, -2343456, 1, -2343456} is"); Console.WriteLine(" = {0}" 26 | , TestSet28.allValuesTheSame(new int[] { 1, -2343456, 1, -2343456 })); 27 | 28 | Console.Write("allValuesTheSame of {0, 0, 0, 0, -1} is"); Console.WriteLine(" = {0}" 29 | , TestSet28.allValuesTheSame(new int[] { 0, 0, 0, 0, -1 })); 30 | 31 | 32 | Console.Write("allValuesTheSame of {432123456} is"); Console.WriteLine(" = {0}" 33 | , TestSet28.allValuesTheSame(new int[] { 432123456 })); 34 | 35 | Console.Write("allValuesTheSame of {-432123456} is"); Console.WriteLine(" = {0}" 36 | , TestSet28.allValuesTheSame(new int[] { -432123456 })); 37 | 38 | Console.Write("allValuesTheSame of {} is"); Console.WriteLine(" = {0}" 39 | , TestSet28.allValuesTheSame(new int[] { })); 40 | 41 | Console.WriteLine("========================"); 42 | 43 | } 44 | 45 | public static void hasNValues() 46 | { 47 | Console.WriteLine("test for hasNValues "); 48 | Console.WriteLine("========================"); 49 | 50 | Console.Write("hasNValues of {1, 2, 2, 1} and 2 is"); Console.WriteLine(" = {0}" 51 | , TestSet28.hasNValues(new int[] { 1, 2, 2, 1 }, 2)); 52 | 53 | Console.Write("hasNValues of {1, 1, 1, 8, 1, 1, 1, 3, 3} and 3 is"); Console.WriteLine(" = {0}" 54 | , TestSet28.hasNValues(new int[] { 1, 1, 1, 8, 1, 1, 1, 3, 3 }, 3)); 55 | 56 | Console.Write("hasNValues of {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10} and 10 is"); Console.WriteLine(" = {0}" 57 | , TestSet28.hasNValues(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 10)); 58 | 59 | 60 | Console.Write("hasNValues of {1, 2, 2, 1} and 3 is"); Console.WriteLine(" = {0}" 61 | , TestSet28.hasNValues(new int[] { 1, 2, 2, 1 }, 3)); 62 | 63 | Console.Write("hasNValues of {1, 1, 1, 8, 1, 1, 1, 3, 3} and 2 is"); Console.WriteLine(" = {0}" 64 | , TestSet28.hasNValues(new int[] { 1, 1, 1, 8, 1, 1, 1, 3, 3 }, 2)); 65 | 66 | Console.Write("hasNValues of {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10} and 20 is"); Console.WriteLine(" = {0}" 67 | , TestSet28.hasNValues(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 20)); 68 | 69 | Console.WriteLine("========================"); 70 | 71 | } 72 | 73 | public static void sameNumberOfFactors() 74 | { 75 | TestSet10.TestSet10Test.sameNumberOfFactors(); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /MUMSPT/TestSet3/TestSet3Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet4 8 | { 9 | public static class TestSet3Test 10 | { 11 | public static void stantonMeasure() 12 | { 13 | Console.WriteLine("test for stantonMeasure "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("array {1, 4, 3, 2, 1, 2, 3, 2} is"); Console.WriteLine(" = {0}" 17 | , TestSet3.stantonMeasure(new int[] { 1, 4, 3, 2, 1, 2, 3, 2 })); 18 | 19 | Console.Write("array {1} is"); Console.WriteLine(" = {0}" 20 | , TestSet3.stantonMeasure(new int[] { 1 })); 21 | 22 | Console.Write("array {0} is"); Console.WriteLine(" = {0}" 23 | , TestSet3.stantonMeasure(new int[] { 0 })); 24 | 25 | Console.Write("array {3, 1, 1, 4} is"); Console.WriteLine(" = {0}" 26 | , TestSet3.stantonMeasure(new int[] { 3, 1, 1, 4 })); 27 | 28 | Console.Write("array {1, 3, 1, 1, 3, 3, 2, 3, 3, 3, 4} is"); Console.WriteLine(" = {0}" 29 | , TestSet3.stantonMeasure(new int[] { 1, 3, 1, 1, 3, 3, 2, 3, 3, 3, 4 })); 30 | 31 | Console.Write("array {} is"); Console.WriteLine(" = {0}" 32 | , TestSet3.stantonMeasure(new int[] { })); 33 | 34 | Console.WriteLine("========================"); 35 | 36 | } 37 | 38 | public static void sumFactor() 39 | { 40 | Console.WriteLine("test for sumFactor "); 41 | Console.WriteLine("========================"); 42 | 43 | Console.Write("array {1, -1, 1, -1, 1, -1, 1} is"); Console.WriteLine(" = {0}" 44 | , TestSet3.sumFactor(new int[] { 1, -1, 1, -1, 1, -1, 1 })); 45 | 46 | Console.Write("array {1, 2, 3, 4} is"); Console.WriteLine(" = {0}" 47 | , TestSet3.sumFactor(new int[] { 1, 2, 3, 4 })); 48 | 49 | Console.Write("array {3, 0, 2, -5, 0} is"); Console.WriteLine(" = {0}" 50 | , TestSet3.sumFactor(new int[] { 3, 0, 2, -5, 0 })); 51 | 52 | Console.Write("array {9, -3, -3, -1, -1} is"); Console.WriteLine(" = {0}" 53 | , TestSet3.sumFactor(new int[] { 9, -3, -3, -1, -1 })); 54 | 55 | Console.Write("array {1} is"); Console.WriteLine(" = {0}" 56 | , TestSet3.sumFactor(new int[] { 1 })); 57 | 58 | Console.Write("array {0, 0, 0} is"); Console.WriteLine(" = {0}" 59 | , TestSet3.sumFactor(new int[] { 0, 0, 0 })); 60 | 61 | Console.WriteLine("========================"); 62 | 63 | } 64 | 65 | public static void guthrieIndex() 66 | { 67 | Console.WriteLine("test for guthrieIndex "); 68 | Console.WriteLine("========================"); 69 | 70 | Console.Write("guthrieIndex of 7 is"); Console.WriteLine(" = {0}" 71 | , TestSet3.guthrieIndex(7)); 72 | 73 | Console.Write("guthrieIndex of 1 is"); Console.WriteLine(" = {0}" 74 | , TestSet3.guthrieIndex(1)); 75 | 76 | 77 | Console.Write("guthrieIndex of 2 is"); Console.WriteLine(" = {0}" 78 | , TestSet3.guthrieIndex(2)); 79 | 80 | Console.Write("guthrieIndex of 3 is"); Console.WriteLine(" = {0}" 81 | , TestSet3.guthrieIndex(3)); 82 | 83 | 84 | Console.Write("guthrieIndex of 4 is"); Console.WriteLine(" = {0}" 85 | , TestSet3.guthrieIndex(4)); 86 | 87 | Console.Write("guthrieIndex of 42 is"); Console.WriteLine(" = {0}" 88 | , TestSet3.guthrieIndex(42)); 89 | } 90 | 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /MUMSPT/TestSet5/TestSet5.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet5 8 | { 9 | public static class TestSet5 10 | { 11 | /// 12 | /// A perfect number is one that is the sum of its factors, 13 | /// excluding itself. The 1st perfect number is 6 because 6 = 1 + 2 + 3. 14 | /// The 2nd perfect number is 28 which equals 1 + 2 + 4 + 7 + 14. 15 | /// The third is 496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248. 16 | /// In each case, the number is the sum of all its factors excluding itself. 17 | /// 18 | /// 19 | /// 20 | /// 21 | public static int henry(int i, int j) 22 | { 23 | int henry = 0,currentperfect = 0; 24 | int iperfect = 0, jperfect = 0, perfectindex = 0; 25 | int n = 1; 26 | do 27 | { 28 | for (int k = 1; k < n; k++) 29 | { 30 | if (n % k == 0) 31 | { 32 | currentperfect += k; 33 | } 34 | } 35 | if(currentperfect == n) 36 | { 37 | perfectindex++; 38 | if(perfectindex == i) 39 | { 40 | iperfect = n; 41 | } 42 | if(perfectindex == j) 43 | { 44 | jperfect = n; 45 | } 46 | } 47 | n++; 48 | currentperfect = 0; 49 | henry = iperfect + jperfect; 50 | } while (henry == 0 || iperfect == 0 || jperfect == 0); 51 | 52 | return henry; 53 | } 54 | 55 | 56 | /// 57 | /// Write a method named isDivisible that takes an integer 58 | /// array and a divisor and returns 1 if all its elements 59 | /// are divided by the divisor with no remainder. Otherwise it returns 0. 60 | /// 61 | /// 62 | /// 63 | /// 64 | public static int isDivisible(int[] a, int divisor) 65 | { 66 | int isDivisible = 1; 67 | 68 | for (int i = 0; i < a.Length && isDivisible == 1; i++) 69 | { 70 | if(a[i] % divisor != 0) 71 | { 72 | isDivisible = 0; 73 | } 74 | } 75 | return isDivisible; 76 | 77 | } 78 | 79 | /// 80 | /// An array is defined to be n-unique if exactly one pair of its elements sum to n. 81 | /// For example, the array {2, 7, 3, 4} is 5-unique because only a[0] and a[2] sum to 5. 82 | /// But the array {2, 3, 3, 7} is not 5-unique because a[0] + a[1] = 5 and a[0] + a[2] = 5. 83 | /// 84 | /// 85 | /// 86 | /// 87 | public static int isNUnique(int[] a, int n) 88 | { 89 | int isNUnique = 0, occurCount = 0; 90 | 91 | for (int i = 0; i < a.Length && occurCount < 2; i++) 92 | { 93 | for (int j = i+1; j < a.Length && occurCount<2; j++) 94 | { 95 | if(a[i]+a[j] == n) 96 | { 97 | occurCount++; 98 | } 99 | 100 | } 101 | } 102 | 103 | return isNUnique = (occurCount == 1 ? 1 : 0); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /MUMSPT/TestSet3/TestSet3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet4 8 | { 9 | public static class TestSet3 10 | { 11 | /// 12 | /// The Stanton measure of an array is computed as follows. 13 | /// Count the number of 1s in the array. Let this count be n. 14 | /// The Stanton measure is the number of times that n appears in the array. 15 | /// For example, the Stanton measure of {1, 4, 3, 2, 1, 2, 3, 2} 16 | /// is 3 because 1 occurs 2 times in the array and 2 occurs 3 times. 17 | /// 18 | /// 19 | /// 20 | public static int stantonMeasure(int[] a) 21 | { 22 | int oneCount = 0,occurCount = 0; 23 | for (int i = 0; i < a.Length; i++) 24 | { 25 | if(a[i] == 1) 26 | { 27 | oneCount++; 28 | } 29 | } 30 | for (int i = 0; i < a.Length; i++) 31 | { 32 | if (a[i] == oneCount) occurCount++; 33 | } 34 | 35 | return occurCount; 36 | 37 | } 38 | 39 | /// 40 | /// The sum factor of an array is defined to be the number 41 | /// of times that the sum of the array appears as an element of the array. 42 | /// So the sum factor of {1, -1, 1, -1, 1, -1, 1} is 4 43 | /// because the sum of the elements of the array is 1 and 1 appears four times in the array. 44 | /// And the sum factor of {1, 2, 3, 4} is 0 45 | /// because the sum of the elements of the array is 10 and 10 does not occur as an element of the array. 46 | /// The sum factor of the empty array { } is defined to be 0. 47 | /// 48 | /// 49 | /// 50 | public static int sumFactor(int[] a) 51 | { 52 | int sum = 0, sumfactor = 0; 53 | 54 | for (int i = 0; i < a.Length; i++) 55 | { 56 | sum += a[i]; 57 | } 58 | 59 | for (int i = 0; i < a.Length; i++) 60 | { 61 | if (a[i] == sum) sumfactor++; 62 | 63 | } 64 | return sumfactor; 65 | } 66 | 67 | 68 | /// 69 | /// The Guthrie index of a positive number n is defined to be how many 70 | /// iterations of the above algorithm it takes before n becomes 1. 71 | /// For example, the Guthrie index of the number 7 is 16 72 | /// because the following sequence is 16 numbers long. 73 | /// 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 74 | /// It is easy to see that this sequence was generated by the above algorithm. 75 | /// Since 7 is odd multiply by 3 and add 1 to get 22 76 | /// which is the first number of the sequence. 77 | /// Since 22 is even, divide by 2 to get 11 which is the second number of the sequence. 78 | /// 11 is odd so multiply by 3 and 79 | /// add 1 to get 34 which is the third number of the sequence and so on. 80 | /// 81 | /// 82 | /// 83 | public static int guthrieIndex(int n) 84 | { 85 | int gindex = 0; 86 | 87 | 88 | while (n !=1) 89 | { 90 | if(n % 2== 0) 91 | { 92 | n = n / 2; 93 | } 94 | else 95 | { 96 | n = n * 3 + 1; 97 | } 98 | gindex++; 99 | } 100 | 101 | return gindex; 102 | } 103 | 104 | 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /MUMSPT/TestSet20/TestSet20.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet20 8 | { 9 | public static class TestSet20 10 | { 11 | /// 12 | /// An onion array is an array that satisfies the following condition for all values of j and k: 13 | /// if j>=0 and k>=0 and j+k=length of array and j!=k then a[j]+a[k]<=10 14 | /// 15 | /// 16 | /// 17 | public static int isOnionArray(int[] a) 18 | { 19 | int isOnion = 1; 20 | if (a.Length > 0) 21 | { 22 | int i = 0, k = a.Length - 1; 23 | while (i != k && isOnion == 1) 24 | { 25 | if (a[i] + a[k] > 10) isOnion = 0; 26 | i++; 27 | k--; 28 | } 29 | } 30 | 31 | return isOnion; 32 | } 33 | 34 | 35 | /// 36 | /// A number n is called prime happy if there 37 | /// is at least one prime less than n and the 38 | /// sum of all primes less than n is evenly 39 | /// divisible by n. 40 | /// Recall that a prime number is an integer > 1 41 | /// which has only two integer factors, 1 and itself 42 | /// 43 | /// 44 | /// 45 | /// 46 | public static int isPrimeHappy(int n) 47 | { 48 | return TestSet11.TestSet11.isPrimeHappy(n); 49 | } 50 | 51 | 52 | /// 53 | /// An integer number can be encoded as an array as follows. 54 | /// Each digit n of the number is represented by n zeros followed by a 1. 55 | /// So the digit 5 is represented by 0, 0, 0, 0, 0, 1. 56 | /// The encodings of each digit of a number are combined to form the encoding of the number. 57 | /// So the number 1234 is encoded as the array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. 58 | /// The first 0, 1 is contributed by the digit 1, 59 | /// the next 0, 0, 1 is contributed by the digit 2, and so on. 60 | /// There is one other encoding rule: if the number is negative, 61 | /// the first element of the encoded array must be -1, so -201 is encoded as {-1, 0, 0, 1, 1, 0, 1}. 62 | /// Note that the 0 digit is represented by no zeros, i.e. there are two consecutive ones! 63 | /// 64 | /// 65 | /// 66 | public static int[] encodeArray(int n) 67 | { 68 | int arrayLength = 0; 69 | int positive = 0; 70 | if (n < 0) 71 | { 72 | positive = -1; 73 | } 74 | int[] encodeArray; 75 | int tempn = Math.Abs(n); 76 | do 77 | { 78 | int lastdigit = tempn % 10; 79 | arrayLength += lastdigit + 1; 80 | tempn = tempn / 10; 81 | } while (tempn >= 1); 82 | 83 | if (positive < 0) 84 | { 85 | arrayLength++; 86 | } 87 | 88 | encodeArray = new int[arrayLength]; 89 | int index = arrayLength; 90 | if (positive < 0) 91 | { 92 | encodeArray[0] = -1; 93 | } 94 | 95 | 96 | tempn = Math.Abs(n); 97 | 98 | do 99 | { 100 | int lastdigit = tempn % 10; 101 | 102 | 103 | encodeArray[index - 1] = 1; 104 | 105 | index -= lastdigit + 1; 106 | tempn = tempn / 10; 107 | } while (tempn >= 1); 108 | return encodeArray; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /MUMSPT/TestSet31/TestSet31Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet31 8 | { 9 | public static class TestSet31Test 10 | { 11 | public static void isDual() 12 | { 13 | Console.WriteLine("test for isDual "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isDual of {1, 2, 3, 0}  is"); Console.WriteLine(" = {0}" 17 | , TestSet31.isDual(new int[] { 1, 2, 3, 0 })); 18 | 19 | Console.Write("isDual of {1, 2, 2, 1, 3, 0}  is"); Console.WriteLine(" = {0}" 20 | , TestSet31.isDual(new int[] { 1, 2, 2, 1, 3, 0 })); 21 | 22 | 23 | Console.Write("isDual of {1, 1, 2, 2}  is"); Console.WriteLine(" = {0}" 24 | , TestSet31.isDual(new int[] { 1, 1, 2, 2 })); 25 | 26 | Console.Write("isDual of {1, 2, 1}  is"); Console.WriteLine(" = {0}" 27 | , TestSet31.isDual(new int[] { 1, 2, 1 })); 28 | 29 | Console.Write("isDual of {}  is"); Console.WriteLine(" = {0}" 30 | , TestSet31.isDual(new int[] { })); 31 | 32 | 33 | 34 | Console.WriteLine("========================"); 35 | } 36 | 37 | public static void isAllPossibilities() 38 | { 39 | Console.WriteLine("test for isAllPossibilities "); 40 | Console.WriteLine("========================"); 41 | 42 | Console.Write("isAllPossibilities of {1, 2, 0, 3} is"); Console.WriteLine(" = {0}" 43 | , TestSet31.isAllPossibilities(new int[] { 1, 2, 0, 3 })); 44 | 45 | Console.Write("isAllPossibilities of {3, 2, 1, 0} is"); Console.WriteLine(" = {0}" 46 | , TestSet31.isAllPossibilities(new int[] { 3, 2, 1, 0 })); 47 | 48 | Console.Write("isAllPossibilities of {1, 2, 4, 3} is"); Console.WriteLine(" = {0}" 49 | , TestSet31.isAllPossibilities(new int[] { 1, 2, 4, 3 })); 50 | 51 | Console.Write("isAllPossibilities of {0, 2, 3} is"); Console.WriteLine(" = {0}" 52 | , TestSet31.isAllPossibilities(new int[] { 0, 2, 3 })); 53 | 54 | 55 | Console.Write("isAllPossibilities of {0} is"); Console.WriteLine(" = {0}" 56 | , TestSet31.isAllPossibilities(new int[] { 0 })); 57 | 58 | Console.Write("isAllPossibilities of {} is"); Console.WriteLine(" = {0}" 59 | , TestSet31.isAllPossibilities(new int[] { })); 60 | 61 | Console.WriteLine("========================"); 62 | 63 | } 64 | 65 | public static void isLayered() 66 | { 67 | Console.WriteLine("test for isLayered "); 68 | Console.WriteLine("========================"); 69 | 70 | Console.Write("isLayered of {1, 1, 2, 2, 2, 3, 3}  is"); Console.WriteLine(" = {0}" 71 | , TestSet31.isLayered(new int[] { 1, 1, 2, 2, 2, 3, 3 })); 72 | 73 | Console.Write("isLayered of {3, 3, 3, 3, 3, 3, 3}  is"); Console.WriteLine(" = {0}" 74 | , TestSet31.isLayered(new int[] { 3, 3, 3, 3, 3, 3, 3 })); 75 | 76 | Console.Write("isLayered of {1, 2, 2, 2, 3, 3}  is"); Console.WriteLine(" = {0}" 77 | , TestSet31.isLayered(new int[] { 1, 2, 2, 2, 3, 3 })); 78 | 79 | Console.Write("isLayered of {2, 2, 2, 3, 3, 1, 1}  is"); Console.WriteLine(" = {0}" 80 | , TestSet31.isLayered(new int[] { 2, 2, 2, 3, 3, 1, 1 })); 81 | 82 | Console.Write("isLayered of {2} is"); Console.WriteLine(" = {0}" 83 | , TestSet31.isLayered(new int[] { 2 })); 84 | 85 | Console.Write("isLayered of {} is"); Console.WriteLine(" = {0}" 86 | , TestSet31.isLayered(new int[] { })); 87 | 88 | Console.WriteLine("========================"); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /MUMSPT/TestSet7/TestSet7.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet7 8 | { 9 | public static class TestSet7 10 | { 11 | /// 12 | /// A simple pattern match on the elements of an array A can be 13 | /// defined using another array P. Each element n of P 14 | /// is negative or positive (never zero) and defines the 15 | /// number of elements in a sequence in A. The first sequence 16 | /// in A starts at A[0] and its length is defined by P[0]. The 17 | /// second sequence follows the first sequence and its length is 18 | /// defined by P[1] and so on. Furthermore, for n in P, if n is 19 | /// positive then the sequence of n elements of A must all be positive. 20 | /// Otherwise the sequence of abs(n) elements must all be negative. 21 | /// The sum of the absolute values of the elements of P must be the length of A. 22 | /// For example, consider the array 23 | /// 24 | /// 25 | /// 26 | /// 27 | public static int matches(int[] a, int[] p) 28 | { 29 | bool isNegative = false; 30 | int isMatches = 1, upperindex = 0,lowinddex = 0; 31 | for (int i = 0; i < p.Length && isMatches==1; i++) 32 | { 33 | isNegative = (p[i] < 0); 34 | upperindex = lowinddex + Math.Abs(p[i]); 35 | 36 | for (int j = lowinddex; j < upperindex && isMatches ==1; j++) 37 | { 38 | if (isNegative) 39 | { 40 | if (a[j] > 0) isMatches = 0; 41 | } 42 | else 43 | { 44 | if (a[j] < 0) isMatches = 0; 45 | } 46 | } 47 | lowinddex = upperindex; 48 | } 49 | 50 | return isMatches; 51 | } 52 | 53 | /// 54 | /// Define a stacked number to be a number that is the sum of the first n 55 | /// positive integers for some n. The first 5 stacked numbers are 56 | /// 1 = 1 57 | /// 3 = 1 + 2 58 | /// 6 = 1 + 2 + 3 59 | /// 10 = 1 + 2 + 3+ 4 60 | /// 15 = 1 + 2 + 3 + 4 + 5 61 | /// 62 | /// 63 | /// 64 | public static int isStacked(int n) 65 | { 66 | int isStacked = 0; 67 | int sum = 0; 68 | int index = 0; 69 | do 70 | { 71 | sum += index; 72 | if (sum == n) isStacked = 1; 73 | index++; 74 | 75 | } while (sum <= n); 76 | 77 | return isStacked; 78 | } 79 | 80 | 81 | /// 82 | /// Define an array to be sum-safe if none of its elements is equal to the sum of its elements. 83 | /// The array a = {5, -5, 0} is not sum-safe because the sum of 84 | /// its elements is 0 and a[2] == 0. However, the array a = {5, -2, 1} is 85 | /// sum-safe because the sum of its elements is 4 and none of its elements equal 4. 86 | /// 87 | /// 88 | /// 89 | public static int isSumSafe(int[] a) 90 | { 91 | int isSumSafe = 1; 92 | int sum = 0; 93 | for (int i = 0; i < a.Length; i++) 94 | { 95 | sum += a[i]; 96 | } 97 | 98 | for (int i = 0; i < a.Length && isSumSafe==1; i++) 99 | { 100 | if (sum == a[i]) isSumSafe = 0; 101 | } 102 | 103 | return isSumSafe; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /MUMSPT/TestSet11/TestSet11.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet11 8 | { 9 | public static class TestSet11 10 | { 11 | /// 12 | /// Define an array to be a Martian array if the number of 13 | /// 1s is greater than the number of 2s and no two adjacent 14 | /// elements are equal. Write a function named isMartian that 15 | /// returns 1 if its array argument is a Martian array, 16 | /// otherwise it returns 0. 17 | /// 18 | /// 19 | /// 20 | public static int isMartian(int[] a) 21 | { 22 | int isMartian = 1; 23 | int oneCount = 0, TwoCount = 0; 24 | for (int i = 0; i < a.Length && isMartian == 1; i++) 25 | { 26 | if (a[i] == 1) oneCount++; 27 | else if (a[i] == 2) TwoCount++; 28 | else 29 | { 30 | if (i < a.Length - 1) // chek if not last item 31 | { 32 | if (a[i] == a[i + 1]) isMartian = 0; 33 | } 34 | } 35 | } 36 | if (oneCount <= TwoCount) isMartian = 0; 37 | return isMartian; 38 | } 39 | 40 | 41 | /// 42 | /// The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 43 | /// The first and second numbers are 1 and after that ni = ni-2 + ni-1, 44 | /// e.g., 34 = 13 + 21. A number in the sequence is called a Fibonacci number. 45 | /// Write a method with signature int closestFibonacci(int n) which returns the largest 46 | /// Fibonacci number that is less than or equal to its argument. For example, 47 | /// closestFibonacci(12) returns 8 because 8 is the largest Fibonacci number less 48 | /// than 12 and closestFibonacci(33) returns 21 because 21 is the largest Fibonacci 49 | /// number that is <= 33. closestFibonacci(34) should return 34. If the argument 50 | /// is less than 1 return 0. Your solution must not use recursion because unless you 51 | /// cache the Fibonacci numbers as you find them, the recursive solution recomputes 52 | /// the same Fibonacci number many times. 53 | /// 54 | /// 55 | /// 56 | public static int closestFibonacci(int n) 57 | { 58 | int closestFibonacci = 0; 59 | if (n > 0) 60 | { 61 | int a = 0, b = 1, c = a + b; 62 | 63 | do 64 | { 65 | a = b; 66 | b = c; 67 | c = a + b; 68 | if (c >= n) 69 | { 70 | closestFibonacci = b; 71 | } 72 | } while (c <= n); 73 | } 74 | return closestFibonacci; 75 | } 76 | 77 | /// 78 | /// A number n is called prime-happy if there is at least one prime 79 | /// less than n and the sum of all primes less than n is evenly divisible by n. 80 | /// 81 | /// 82 | /// 83 | public static int isPrimeHappy(int n) 84 | { 85 | int isPrimeHappy = 1; 86 | int count = 0, sum = 0; 87 | 88 | for (int i = 0; i < n; i++) 89 | { 90 | if (Helper.isPrime(i)) 91 | { 92 | count++; 93 | sum += i; 94 | } 95 | } 96 | 97 | if (count < 1) isPrimeHappy = 0; 98 | if (sum % n != 0) isPrimeHappy = 0; 99 | 100 | return isPrimeHappy; 101 | 102 | 103 | } 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /MUMSPT/TestSet21/TestSet21Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet21 8 | { 9 | public static class TestSet21Test 10 | { 11 | public static void isSystematicallyIncreasing() 12 | { 13 | Console.WriteLine("test for isSystematicallyIncreasing "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isSystematicallyIncreasing of {1} is"); Console.WriteLine(" = {0}" 17 | , TestSet21.isSystematicallyIncreasing(new int[] { 1 })); 18 | 19 | Console.Write("isSystematicallyIncreasing of {1, 2, 1, 2, 3} is"); Console.WriteLine(" = {0}" 20 | , TestSet21.isSystematicallyIncreasing(new int[] { 1, 2, 1, 2, 3 })); 21 | 22 | Console.Write("isSystematicallyIncreasing of {1, 1, 3} is"); Console.WriteLine(" = {0}" 23 | , TestSet21.isSystematicallyIncreasing(new int[] { 1, 1, 3 })); 24 | 25 | Console.Write("isSystematicallyIncreasing of {1, 2, 1, 2, 1, 2} is"); Console.WriteLine(" = {0}" 26 | , TestSet21.isSystematicallyIncreasing(new int[] { 1, 2, 1, 2, 1, 2 })); 27 | 28 | 29 | Console.Write("isSystematicallyIncreasing of {1, 2, 3, 1, 2, 1} is"); Console.WriteLine(" = {0}" 30 | , TestSet21.isSystematicallyIncreasing(new int[] { 1, 2, 3, 1, 2, 1 })); 31 | 32 | 33 | Console.Write("isSystematicallyIncreasing of {1, 1, 2, 3} is"); Console.WriteLine(" = {0}" 34 | , TestSet21.isSystematicallyIncreasing(new int[] { 1, 1, 2, 3 })); 35 | 36 | 37 | Console.WriteLine("========================"); 38 | } 39 | 40 | 41 | public static void isFactorialPrime() 42 | { 43 | Console.WriteLine("test for isFactorialPrime "); 44 | Console.WriteLine("========================"); 45 | 46 | Console.Write("isFactorialPrime of 2 is"); Console.WriteLine(" = {0}" 47 | , TestSet21.isFactorialPrime(2)); 48 | 49 | Console.Write("isFactorialPrime of 3 is"); Console.WriteLine(" = {0}" 50 | , TestSet21.isFactorialPrime(3)); 51 | 52 | Console.Write("isFactorialPrime of 7 is"); Console.WriteLine(" = {0}" 53 | , TestSet21.isFactorialPrime(7)); 54 | 55 | Console.Write("isFactorialPrime of 8 is"); Console.WriteLine(" = {0}" 56 | , TestSet21.isFactorialPrime(8)); 57 | 58 | Console.Write("isFactorialPrime of 11 is"); Console.WriteLine(" = {0}" 59 | , TestSet21.isFactorialPrime(11)); 60 | 61 | Console.Write("isFactorialPrime of 721 is"); Console.WriteLine(" = {0}" 62 | , TestSet21.isFactorialPrime(721)); 63 | 64 | 65 | 66 | 67 | Console.WriteLine("========================"); 68 | } 69 | 70 | 71 | public static void largestDifferenceOfEvens() 72 | { 73 | Console.WriteLine("test for largestDifferenceOfEvens "); 74 | Console.WriteLine("========================"); 75 | 76 | Console.Write("largestDifferenceOfEvens of {-2, 3, 4, 9} is"); Console.WriteLine(" = {0}" 77 | , TestSet21.largestDifferenceOfEvens(new int[] { -2, 3, 4, 9 })); 78 | 79 | Console.Write("largestDifferenceOfEvens of {1, 3, 5, 9} is"); Console.WriteLine(" = {0}" 80 | , TestSet21.largestDifferenceOfEvens(new int[] { 1, 3, 5, 9 })); 81 | 82 | Console.Write("largestDifferenceOfEvens of {1, 18, 5, 7, 33} is"); Console.WriteLine(" = {0}" 83 | , TestSet21.largestDifferenceOfEvens(new int[] { 1, 18, 5, 7, 33 })); 84 | 85 | Console.Write("largestDifferenceOfEvens of {2, 2, 2, 2} is"); Console.WriteLine(" = {0}" 86 | , TestSet21.largestDifferenceOfEvens(new int[] { 2, 2, 2, 2 })); 87 | 88 | Console.Write("largestDifferenceOfEvens of {1, 2, 1, 2, 1, 4, 1, 6, 4} is"); Console.WriteLine(" = {0}" 89 | , TestSet21.largestDifferenceOfEvens(new int[] { 1, 2, 1, 2, 1, 4, 1, 6, 4 })); 90 | 91 | 92 | Console.WriteLine("========================"); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /MUMSPT/TestSet4/TestSet4Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet4 8 | { 9 | public static class TestSet4Test 10 | { 11 | 12 | public static void solve10() 13 | { 14 | Console.WriteLine("test for solve10 "); 15 | Console.WriteLine("========================"); 16 | 17 | 18 | 19 | Console.Write("solve10 of x is"); Console.WriteLine(" = {0}" 20 | , TestSet4.solve10()[0]); 21 | 22 | Console.Write("solve10 of y is"); Console.WriteLine(" = {0}" 23 | , TestSet4.solve10()[1]); 24 | 25 | 26 | 27 | Console.WriteLine("========================"); 28 | } 29 | 30 | public static void solve101() 31 | { 32 | Console.WriteLine("test for solve101 "); 33 | Console.WriteLine("========================"); 34 | 35 | 36 | Console.Write("solve10 of x is"); Console.WriteLine(" = {0}" 37 | , TestSet4.solve101()[0]); 38 | 39 | Console.Write("solve10 of y is"); Console.WriteLine(" = {0}" 40 | , TestSet4.solve101()[1]); 41 | 42 | 43 | 44 | 45 | Console.WriteLine("========================"); 46 | } 47 | 48 | public static void repsEqual() 49 | { 50 | Console.WriteLine("test for repsEqual "); 51 | Console.WriteLine("========================"); 52 | 53 | Console.Write("array { 3, 2, 0, 5, 3 } and 32053 is"); Console.WriteLine(" = {0}" 54 | , TestSet4.repsEqual(new int[] { 3, 2, 0, 5, 3 }, 32053)); 55 | 56 | Console.Write("array {3, 2, 0, 5} and 32053 is"); Console.WriteLine(" = {0}" 57 | , TestSet4.repsEqual(new int[] { 3, 2, 0, 5 }, 32053)); 58 | 59 | Console.Write("array {3, 2, 0, 5, 3, 4} and 32053 is"); Console.WriteLine(" = {0}" 60 | , TestSet4.repsEqual(new int[] { 3, 2, 0, 5, 3, 4 }, 32053)); 61 | 62 | Console.Write("array {2, 3, 0, 5, 3} and 32053 is"); Console.WriteLine(" = {0}" 63 | , TestSet4.repsEqual(new int[] { 2, 3, 0, 5, 3 }, 32053)); 64 | 65 | Console.Write("array {9, 3, 1, 1, 2} and 32053 is"); Console.WriteLine(" = {0}" 66 | , TestSet4.repsEqual(new int[] { 9, 3, 1, 1, 2 }, 32053)); 67 | 68 | 69 | Console.Write("array {0, 3, 2, 0, 5, 3} and 32053 is"); Console.WriteLine(" = {0}" 70 | , TestSet4.repsEqual(new int[] { 0, 3, 2, 0, 5, 3 }, 32053)); 71 | 72 | Console.WriteLine("========================"); 73 | 74 | 75 | } 76 | 77 | public static void isCentered15() 78 | { 79 | Console.WriteLine("test for isCentered15 "); 80 | Console.WriteLine("========================"); 81 | 82 | Console.Write("array {3, 2, 10, 4, 1, 6, 9} is"); Console.WriteLine(" = {0}" 83 | , TestSet4.isCentered15(new int[] { 3, 2, 10, 4, 1, 6, 9 })); 84 | 85 | Console.Write("array {2, 10, 4, 1, 6, 9} is"); Console.WriteLine(" = {0}" 86 | , TestSet4.isCentered15(new int[] { 2, 10, 4, 1, 6, 9 })); 87 | 88 | Console.Write("array {3, 2, 10, 4, 1, 6} is"); Console.WriteLine(" = {0}" 89 | , TestSet4.isCentered15(new int[] { 3, 2, 10, 4, 1, 6 })); 90 | 91 | Console.Write("array {1,1,8, 3, 1, 1} is"); Console.WriteLine(" = {0}" 92 | , TestSet4.isCentered15(new int[] { 1, 1, 8, 3, 1, 1 })); 93 | 94 | Console.Write("array {9, 15, 6} is"); Console.WriteLine(" = {0}" 95 | , TestSet4.isCentered15(new int[] { 9, 15, 6 })); 96 | 97 | Console.Write("array {1, 1, 2, 2, 1, 1} is"); Console.WriteLine(" = {0}" 98 | , TestSet4.isCentered15(new int[] { 1, 1, 2, 2, 1, 1 })); 99 | 100 | Console.Write("array {1, 1, 15 -1,-1} is"); Console.WriteLine(" = {0}" 101 | , TestSet4.isCentered15(new int[] { 1, 1, 15 - 1, -1 })); 102 | 103 | Console.WriteLine("========================"); 104 | 105 | 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /MUMSPT/TestSet22/TestSet22.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet22 8 | { 9 | public static class TestSet22 10 | { 11 | /// 12 | /// A hodder number is one that is prime and is 13 | /// equal to 2j-1 for some j. For example, 31 is 14 | /// a hodder number because 31 is prime and is 15 | /// equal to 25-1 (in this case j = 5). 16 | /// The first 4 hodder numbers are 3, 7, 31, 127 17 | /// Write a function with signature int isHodder(int n) 18 | /// that returns 1 if n is a hodder number, otherwise it returns 0. 19 | /// Recall that a prime number is a 20 | /// whole number greater than 1 that has 21 | /// only two whole number factors, itself and 1. 22 | /// 23 | /// 24 | /// 25 | public static int isHodder(int n) 26 | { 27 | int isHodder = 0; 28 | 29 | int calculated = 0, j = 0; 30 | if (Helper.isPrime(n)) 31 | { 32 | do 33 | { 34 | calculated = ((int)Math.Pow(2, j)) - 1; 35 | if (calculated == n) 36 | isHodder = 1; 37 | j++; 38 | } while (calculated <= n && isHodder == 0); 39 | } 40 | 41 | return isHodder; 42 | } 43 | 44 | 45 | /// 46 | /// One word is an anagram of another word 47 | /// if it is a rearrangement of all the letters 48 | /// of the second word. For example, the character 49 | /// arrays {‘s’, ‘i’, ‘t’} and {‘i’, ‘t’, ‘s’} 50 | /// represent words that are anagrams of one 51 | /// another because “its” is a rearrangement of 52 | /// all the letters of “sit” and vice versa. 53 | /// Write a function that accepts two character 54 | /// arrays and returns 1 if they are anagrams 55 | /// of one another, otherwise it returns 0. 56 | /// For simplicity, if the two input character 57 | /// arrays are equal, you may consider them 58 | /// to be anagrams. 59 | /// If you are programming in Java or C#, the function signature is: 60 | /// int areAnagrams(char[] a1, char[] a2) 61 | /// Hint: Please note that “pool” is not an anagram of 62 | /// “poll” even though they use the same letters. 63 | /// Please be sure that your function returns 0 if given these two words! 64 | /// You can use another array to keep track of each 65 | /// letter that is found so that you don’t count the same letter 66 | /// twice (e.g., the attempt to find the second “o” of “pool” in “poll” should fail.) 67 | /// Hint: do not modify either a1 or a2, i.e., 68 | /// your function should have no side effects! 69 | /// If your algorithm requires modification of 70 | /// either of these arrays, you must work with a 71 | /// copy of the array and modify the copy! 72 | /// 73 | /// 74 | /// 75 | /// 76 | public static int areAnagrams(char[] a1, char[] a2) 77 | { 78 | int areAnagrams = 1; 79 | 80 | int[] isFound = new int[a1.Length]; 81 | for (int i = 0; i < a1.Length && areAnagrams==1; i++) 82 | { 83 | int elindex = Helper.isExistInArray(a2, a1[i]); 84 | if (elindex != -1) 85 | { 86 | 87 | isFound[i] = elindex; 88 | } 89 | else 90 | { 91 | areAnagrams = 0; 92 | } 93 | } 94 | return areAnagrams; 95 | } 96 | 97 | 98 | /// 99 | /// Solved in TestSet11 100 | /// 101 | /// 102 | /// 103 | public static int closestFibonacci(int n) 104 | { 105 | return TestSet11.TestSet11.closestFibonacci(n); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /MUMSPT/TestSet2/TestSet2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet2 8 | { 9 | public static class TestSet2 10 | { 11 | /// 12 | /// 14 | /// where x and y are positive, non-zero integers, 15 | /// x,<5, 20> and<4, 5>. 25 | /// ]]> 26 | /// 27 | /// 28 | /// 29 | public static int countSquarePairs(int[] a) 30 | { 31 | int count = 0; 32 | int x = 0, y = 0; 33 | for (int i = 0; i < a.Length ; i++) 34 | { 35 | x = a[i]; 36 | for (int j = 0; j < a.Length && x > 0; j++) 37 | { 38 | y = a[j]; 39 | if (x < y && Helper.isPerfectSquare(x + y)) 40 | count++; 41 | } 42 | } 43 | 44 | return count; 45 | } 46 | 47 | /// 48 | /// A prime number is an integer that is divisible only by 1 and itself. 49 | /// A porcupine number is a prime number whose last digit is 9 50 | /// and the next prime number that follows it also ends with the digit 9. 51 | /// For example 139 is a porcupine number because: 52 | /// 53 | /// 54 | /// A porcupine number 55 | public static int findPorcupineNumber(int n) 56 | { 57 | int porcupine = -1,nextPrime = -1; 58 | do 59 | { 60 | n++; 61 | if (porcupine == -1 && Helper.isPrime(n) && (n % 10 == 9)) 62 | { 63 | porcupine = n; 64 | } 65 | 66 | while (porcupine > -1 && nextPrime == -1) // check for next prime 67 | { 68 | n++; 69 | if (Helper.isPrime(n)) 70 | { 71 | nextPrime = n; 72 | if (nextPrime % 10 != 9) 73 | { 74 | // reset the porcupine and nextprime 75 | porcupine = -1; nextPrime = -1; 76 | } 77 | } 78 | } 79 | } 80 | while (porcupine == -1 && nextPrime == -1); 81 | 82 | return porcupine; 83 | } 84 | 85 | /// 86 | /// The Guthrie sequence of a positive number n is defined to be the numbers generated by the above algorithm. 87 | /// For example, the Guthrie sequence of the number 7 is 88 | /// 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 89 | /// 90 | /// 91 | /// 92 | public static int isGuthrieSequence(int[] a) 93 | { 94 | int isGuthie = 1; 95 | // check for the last element 96 | if (a[a.Length - 1] != 1) return 0; 97 | 98 | int currentnumber = a[0]; 99 | 100 | for (int i = 1; i < a.Length && isGuthie == 1; i++) 101 | { 102 | if (currentnumber % 2 == 0) 103 | { 104 | if (a[i] != currentnumber / 2) isGuthie = 0; 105 | } 106 | else 107 | { 108 | if (a[i] != currentnumber * 3 + 1) isGuthie = 0; 109 | } 110 | currentnumber = a[i]; 111 | } 112 | 113 | return isGuthie; 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /MUMSPT/TestSet19/TestSet19Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet19 8 | { 9 | public static class TestSet19Test 10 | { 11 | public static void isZeroPlentiful() 12 | { 13 | Console.WriteLine("test for isZeroPlentiful "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isZeroPlentiful of {0, 0, 0, 0, 0} is"); Console.WriteLine(" = {0}" 17 | , TestSet19.isZeroPlentiful(new int[] { 0, 0, 0, 0, 0 })); 18 | 19 | Console.Write("isZeroPlentiful of {1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12} is"); Console.WriteLine(" = {0}" 20 | , TestSet19.isZeroPlentiful(new int[] { 1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12 })); 21 | 22 | Console.Write("isZeroPlentiful of {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0} is"); Console.WriteLine(" = {0}" 23 | , TestSet19.isZeroPlentiful(new int[] { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0 })); 24 | 25 | Console.Write("isZeroPlentiful of {1, 2, 3, 4} is"); Console.WriteLine(" = {0}" 26 | , TestSet19.isZeroPlentiful(new int[] { 1, 2, 3, 4 })); 27 | 28 | Console.Write("isZeroPlentiful of {1, 0, 0, 0, 2, 0, 0, 0, 0} is"); Console.WriteLine(" = {0}" 29 | , TestSet19.isZeroPlentiful(new int[] { 1, 0, 0, 0, 2, 0, 0, 0, 0 })); 30 | 31 | Console.Write("isZeroPlentiful of {0} is"); Console.WriteLine(" = {0}" 32 | , TestSet19.isZeroPlentiful(new int[] { 0 })); 33 | Console.Write("isZeroPlentiful of {} is"); Console.WriteLine(" = {0}" 34 | , TestSet19.isZeroPlentiful(new int[] { })); 35 | 36 | 37 | 38 | Console.WriteLine("========================"); 39 | } 40 | 41 | public static void isDigitIncreasing() 42 | { 43 | Console.WriteLine("test for isDigitIncreasing "); 44 | Console.WriteLine("========================"); 45 | 46 | Console.Write("isDigitIncreasing of 24 is"); Console.WriteLine(" = {0}" 47 | , TestSet19.isDigitIncreasing(24)); 48 | 49 | Console.Write("isDigitIncreasing of 7 is"); Console.WriteLine(" = {0}" 50 | , TestSet19.isDigitIncreasing(7)); 51 | 52 | 53 | Console.Write("isDigitIncreasing of 36 is"); Console.WriteLine(" = {0}" 54 | , TestSet19.isDigitIncreasing(36)); 55 | 56 | 57 | Console.Write("isDigitIncreasing of 984 is"); Console.WriteLine(" = {0}" 58 | , TestSet19.isDigitIncreasing(984)); 59 | 60 | 61 | 62 | Console.Write("isDigitIncreasing of 7404 is"); Console.WriteLine(" = {0}" 63 | , TestSet19.isDigitIncreasing(7404)); 64 | 65 | 66 | Console.WriteLine("========================"); 67 | } 68 | 69 | 70 | public static void decodeArray() 71 | { 72 | Console.WriteLine("test for decodeArray "); 73 | Console.WriteLine("========================"); 74 | 75 | Console.Write("decodeArray of {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1} is"); Console.WriteLine(" = {0}" 76 | , TestSet19.decodeArray(new int[] { 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 })); 77 | 78 | Console.Write("decodeArray of {1} is"); Console.WriteLine(" = {0}" 79 | , TestSet19.decodeArray(new int[] { 1 })); 80 | 81 | Console.Write("decodeArray of {0, 1} is"); Console.WriteLine(" = {0}" 82 | , TestSet19.decodeArray(new int[] { 0, 1 })); 83 | 84 | 85 | Console.Write("decodeArray of {-1, 0, 1} is"); Console.WriteLine(" = {0}" 86 | , TestSet19.decodeArray(new int[] { -1, 0, 1 })); 87 | 88 | 89 | Console.Write("decodeArray of {0, 1, 1, 1, 1, 1, 0, 1} is"); Console.WriteLine(" = {0}" 90 | , TestSet19.decodeArray(new int[] { 0, 1, 1, 1, 1, 1, 0, 1 })); 91 | 92 | 93 | Console.Write("decodeArray of {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,1} is"); Console.WriteLine(" = {0}" 94 | , TestSet19.decodeArray(new int[] { 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, 1 })); 95 | 96 | Console.WriteLine("========================"); 97 | } 98 | 99 | 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /MUMSPT/TestSet16/TestSet16.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet16 8 | { 9 | public static class TestSet16 10 | { 11 | /// 12 | /// Write a function named largestAdjacentSum that iterates through an array 13 | /// computing the sum of adjacent elements and returning the largest such sum. 14 | /// You may assume that the array has at least 2 elements. 15 | /// 16 | /// 17 | /// 18 | public static int largestAdjacentSum(int[] a) 19 | { 20 | int sum = 0; 21 | 22 | 23 | for (int i = 0; i < a.Length; i++) 24 | { 25 | int current = 0, next = 0; 26 | current = a[i]; 27 | if (i < a.Length - 1) next = a[i + 1]; 28 | 29 | int temp = current + next; 30 | if (temp > sum) 31 | sum = temp; 32 | } 33 | 34 | return sum; 35 | } 36 | 37 | /// 38 | /// The number 198 has the property that 198 = 11 + 99 + 88, i.e., 39 | /// if each of its digits is concatenated twice and then summed, 40 | /// the result will be the original number. It turns out that 198 is 41 | /// the only number with this property. However, the property can be 42 | /// generalized so that each digit is concatenated n times and then summed. 43 | /// For example, 2997 = 222+999+999+777 and here each 44 | /// digit is concatenated three times. Write a function named 45 | /// checkContenatedSum that tests if a number has this generalized property. 46 | /// The signature of the function is 47 | /// 48 | /// 49 | /// 50 | /// 51 | public static int checkConcatenatedSum(int n, int catlen) 52 | { 53 | int isConcatenatedSum = 1; 54 | int sum = 0, tempn = n; 55 | do 56 | { 57 | int lastdigit = tempn % 10; 58 | int tempsum = 0; 59 | int tens = 1; 60 | for (int i = 1; i <= catlen; i++) 61 | { 62 | 63 | tempsum += (tens * lastdigit); 64 | tens *= 10; 65 | } 66 | 67 | tempn = tempn / 10; 68 | sum += tempsum; 69 | } while (tempn >= 1); 70 | 71 | if (sum != n) isConcatenatedSum = 0; 72 | return isConcatenatedSum; 73 | } 74 | 75 | 76 | /// 77 | /// Define an m-n sequenced array to be an array that contains one or 78 | /// more occurrences of all the integers between m and n inclusive. 79 | /// Furthermore, the array must be in ascending order and contain only those integers. 80 | /// For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced array. 81 | /// The array {2, 2, 3, 5, 5, 5} is not a 2-5 sequenced array because it is missing a 4. 82 | /// The array {0, 2, 2, 3, 3} is not a 2-3 sequenced array because the 0 is out of range. 83 | /// And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order. 84 | /// 85 | /// 86 | /// 87 | /// 88 | /// 89 | public static int isSequencedArray(int[] a, int m, int n) 90 | { 91 | int isSequnced = 1; 92 | int current = 0; 93 | for (int i = 0; i < a.Length && isSequnced==1; i++) 94 | { 95 | current = a[i]; 96 | if (i == 0) if (current != m) isSequnced = 0; 97 | if (i == a.Length - 1) if (current != n) isSequnced = 0; 98 | if (current < m || current > n) isSequnced = 0; 99 | if (i > 0) 100 | if (current 12 | /// Write a function named isSquare that returns 1 13 | /// if its integer argument is a square of some integer, 14 | /// otherwise it returns 0. Your function must not 15 | /// use any function or method (e.g. sqrt) that 16 | /// comes with a runtime library or class library! 17 | /// You will need to write a loop to solve this problem. 18 | /// Furthermore, your method should return as soon as the 19 | /// status of its parameter is known. 20 | /// So once it is known that the input parameter 21 | /// is a square of some integer, 22 | /// your method should return 1 and once it is 23 | /// known that the input is not a square, 24 | /// the method should return 0. 25 | /// There should be no wasted loop cycles, 26 | /// your method should be efficent! 27 | /// 28 | /// 29 | /// 30 | public static int isSquare(int n) 31 | { 32 | int isSquare = 0; 33 | 34 | int square = 0; 35 | for (int i = 0; square <= n && isSquare == 0; i++) 36 | { 37 | square = i * i; 38 | if (square - n == 0) 39 | { 40 | isSquare = 1; 41 | } 42 | } 43 | 44 | return isSquare; 45 | 46 | 47 | } 48 | 49 | 50 | /// 51 | /// An array is called complete if it contains an even element, 52 | /// a perfect square and two different elements that sum to 8. 53 | /// For example, {3, 2, 9, 5} is complete because 2 is even, 54 | /// 9 is a perfect square and a[0] + a[3] = 8. 55 | /// Write a function named isComplete that 56 | /// accepts an integer array and returns 1 if it is a 57 | /// complete array, otherwise it returns 0. 58 | /// Your method must be efficient. 59 | /// It must return as soon as it is known that the array is complete. 60 | /// Hint: reuse the method you wrote for question 1. 61 | /// 62 | /// 63 | /// 64 | public static int isComplete(int[] a) 65 | { 66 | int isComplete = 0; 67 | int isEven = 0, isperfectsquare = 0; 68 | int isSum8 = 0,current=0; 69 | for (int i = 0; i < a.Length && isComplete==0; i++) 70 | { 71 | current = a[i]; 72 | if (current % 2 == 0) isEven = 1; 73 | if (isSquare(current) == 1) 74 | isperfectsquare = 1; 75 | for (int j = i + 1; j < a.Length && isSum8==0; j++) 76 | { 77 | 78 | if (current + a[j] == 8) 79 | isSum8 = 1; 80 | 81 | } 82 | 83 | if (isEven == 1 && isperfectsquare == 1 && isSum8 == 1) 84 | isComplete = 1; 85 | } 86 | 87 | 88 | return isComplete; 89 | } 90 | 91 | 92 | /// 93 | /// Write a function that takes two arguments, 94 | /// an array of integers and a positive, 95 | /// non-zero number n. It sums n elements of the array 96 | /// starting at the beginning of the array. If n is 97 | /// greater than the number of elements in the array, 98 | /// the function loops back to the beginning of 99 | /// the array and continues summing until it 100 | /// has summed n elements. You may assume that 101 | /// the array contains at least one element 102 | /// and that n is greater than 0. 103 | /// 104 | /// 105 | /// 106 | /// 107 | public static int loopSum(int[] a, int n) 108 | { 109 | int sum = 0; 110 | 111 | for (int i = 0; i < n; i++) 112 | { 113 | int index = i % a.Length; 114 | sum += a[index]; 115 | } 116 | return sum; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /MUMSPT/TestSet24/TestSet24Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet24 8 | { 9 | public static class TestSet24Test 10 | { 11 | public static void isTriangular() 12 | { 13 | Console.WriteLine("test for isTriangular "); 14 | Console.WriteLine("========================"); 15 | for (int i = 1; i < 11; i++) 16 | { 17 | 18 | 19 | Console.Write("isTriangular of " + i + " is"); Console.WriteLine(" = {0}" 20 | , TestSet24.isTriangular(i)); 21 | 22 | } 23 | 24 | 25 | Console.WriteLine("========================"); 26 | 27 | } 28 | 29 | public static void isMercurial() 30 | { 31 | Console.WriteLine("test for isMercurial "); 32 | Console.WriteLine("========================"); 33 | 34 | 35 | 36 | Console.Write("isMercurial of {1, 2, 10, 3, 15, 1, 2, 2} is"); Console.WriteLine(" = {0}" 37 | , TestSet24.isMercurial(new int[] { 1, 2, 10, 3, 15, 1, 2, 2 })); 38 | 39 | 40 | Console.Write("isMercurial of {5, 2, 10, 3, 15, 1, 2, 2} is"); Console.WriteLine(" = {0}" 41 | , TestSet24.isMercurial(new int[] { 5, 2, 10, 3, 15, 1, 2, 2 })); 42 | 43 | Console.Write("isMercurial of {1, 2, 10, 3, 15, 16, 2, 2} is"); Console.WriteLine(" = {0}" 44 | , TestSet24.isMercurial(new int[] { 1, 2, 10, 3, 15, 16, 2, 2 })); 45 | 46 | 47 | Console.Write("isMercurial of {3, 2, 18, 1, 0, 3, -11, 1, 3} is"); Console.WriteLine(" = {0}" 48 | , TestSet24.isMercurial(new int[] { 3, 2, 18, 1, 0, 3, -11, 1, 3 })); 49 | 50 | 51 | Console.Write("isMercurial of {2, 3, 1, 1, 18} is"); Console.WriteLine(" = {0}" 52 | , TestSet24.isMercurial(new int[] { 2, 3, 1, 1, 18 })); 53 | 54 | 55 | Console.Write("isMercurial of {8, 2, 1, 1, 18, 3, 5} is"); Console.WriteLine(" = {0}" 56 | , TestSet24.isMercurial(new int[] { 8, 2, 1, 1, 18, 3, 5 })); 57 | 58 | Console.Write("isMercurial of {3, 3, 3, 3, 3, 3} is"); Console.WriteLine(" = {0}" 59 | , TestSet24.isMercurial(new int[] { 3, 3, 3, 3, 3, 3 })); 60 | 61 | Console.Write("isMercurial of {1} is"); Console.WriteLine(" = {0}" 62 | , TestSet24.isMercurial(new int[] { 1 })); 63 | 64 | Console.Write("isMercurial of {} is"); Console.WriteLine(" = {0}" 65 | , TestSet24.isMercurial(new int[] { })); 66 | 67 | Console.WriteLine("========================"); 68 | 69 | } 70 | 71 | public static void is235Array() 72 | { 73 | Console.WriteLine("test for is235Array "); 74 | Console.WriteLine("========================"); 75 | 76 | 77 | 78 | Console.Write("is235Array of {2, 3, 5, 7, 11} is"); Console.WriteLine(" = {0}" 79 | , TestSet24.is235Array(new int[] { 2, 3, 5, 7, 11 })); 80 | 81 | Console.Write("is235Array of {2, 3, 6, 7, 11} is"); Console.WriteLine(" = {0}" 82 | , TestSet24.is235Array(new int[] { 2, 3, 6, 7, 11 })); 83 | 84 | Console.Write("is235Array of {2, 3, 4, 5, 6, 7, 8, 9, 10} is"); Console.WriteLine(" = {0}" 85 | , TestSet24.is235Array(new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 })); 86 | 87 | Console.Write("is235Array of {2, 4, 8, 16, 32} is"); Console.WriteLine(" = {0}" 88 | , TestSet24.is235Array(new int[] { 2, 4, 8, 16, 32 })); 89 | 90 | Console.Write("is235Array of {3, 9, 27, 7, 1, 1, 1, 1, 1} is"); Console.WriteLine(" = {0}" 91 | , TestSet24.is235Array(new int[] { 3, 9, 27, 7, 1, 1, 1, 1, 1 })); 92 | 93 | Console.Write("is235Array of {7, 11, 77, 49} is"); Console.WriteLine(" = {0}" 94 | , TestSet24.is235Array(new int[] { 7, 11, 77, 49 })); 95 | 96 | Console.Write("is235Array of {2} is"); Console.WriteLine(" = {0}" 97 | , TestSet24.is235Array(new int[] { 2})); 98 | 99 | Console.Write("is235Array of {} is"); Console.WriteLine(" = {0}" 100 | , TestSet24.is235Array(new int[] { })); 101 | 102 | Console.Write("is235Array of {7, 2, 7, 2, 7, 2, 7, 2, 3, 7, 7} is"); Console.WriteLine(" = {0}" 103 | , TestSet24.is235Array(new int[] { 7, 2, 7, 2, 7, 2, 7, 2, 3, 7, 7 })); 104 | 105 | 106 | 107 | Console.WriteLine("========================"); 108 | 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /MUMSPT/TestSet8/TestSet8Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet8 8 | { 9 | public static class TestSet8Test 10 | { 11 | public static void isIsolated() 12 | { 13 | Console.WriteLine("test for isIsolated "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isIsolated of 163 is"); Console.WriteLine(" = {0}" 17 | , TestSet8.isIsolated(163)); 18 | 19 | Console.Write("isIsolated of 162 is"); Console.WriteLine(" = {0}" 20 | , TestSet8.isIsolated(162)); 21 | 22 | Console.Write("isIsolated of 63 is"); Console.WriteLine(" = {0}" 23 | , TestSet8.isIsolated(63)); 24 | 25 | Console.Write("isIsolated of 58 is"); Console.WriteLine(" = {0}" 26 | , TestSet8.isIsolated(58)); 27 | 28 | Console.Write("isIsolated of 34 is"); Console.WriteLine(" = {0}" 29 | , TestSet8.isIsolated(34)); 30 | 31 | Console.Write("isIsolated of 28 is"); Console.WriteLine(" = {0}" 32 | , TestSet8.isIsolated(28 )); 33 | 34 | Console.Write("isIsolated of 24 is"); Console.WriteLine(" = {0}" 35 | , TestSet8.isIsolated(24)); 36 | Console.Write("isIsolated of 14 is"); Console.WriteLine(" = {0}" 37 | , TestSet8.isIsolated(14)); 38 | Console.Write("isIsolated of 9 is"); Console.WriteLine(" = {0}" 39 | , TestSet8.isIsolated(9)); 40 | 41 | Console.Write("isIsolated of 10 is"); Console.WriteLine(" = {0}" 42 | , TestSet8.isIsolated(10)); 43 | 44 | Console.Write("isIsolated of 62 is"); Console.WriteLine(" = {0}" 45 | , TestSet8.isIsolated(62)); 46 | 47 | Console.WriteLine("========================"); 48 | } 49 | 50 | public static void isVanilla() 51 | { 52 | Console.WriteLine("test for isVanilla "); 53 | Console.WriteLine("========================"); 54 | 55 | Console.Write("isVanilla of {1, 1, 11, 1111, 1111111} is"); Console.WriteLine(" = {0}" 56 | , TestSet8.isVanilla(new int[] { 1, 1, 11, 1111, 1111111 })); 57 | 58 | Console.Write("isVanilla of {11, 101, 1111, 11111} is"); Console.WriteLine(" = {0}" 59 | , TestSet8.isVanilla(new int[] { 11, 101, 1111, 11111 })); 60 | 61 | Console.Write("isVanilla of {1} is"); Console.WriteLine(" = {0}" 62 | , TestSet8.isVanilla(new int[] { 1 })); 63 | 64 | Console.Write("isVanilla of {11, 22, 13, 34, 125} is"); Console.WriteLine(" = {0}" 65 | , TestSet8.isVanilla(new int[] { 11, 22, 13, 34, 125 })); 66 | 67 | Console.Write("isVanilla of {9, 999, 99999, -9999} is"); Console.WriteLine(" = {0}" 68 | , TestSet8.isVanilla(new int[] { 9, 999, 99999, -9999 })); 69 | 70 | Console.Write("isVanilla of { } is"); Console.WriteLine(" = {0}" 71 | , TestSet8.isVanilla(new int[] { })); 72 | 73 | Console.WriteLine("========================"); 74 | } 75 | 76 | public static void isTrivalent() 77 | { 78 | Console.WriteLine("test for isTrivalent "); 79 | Console.WriteLine("========================"); 80 | 81 | Console.Write("isTrivalent of {22, 19, 10, 10, 19, 22, 22, 10} is"); Console.WriteLine(" = {0}" 82 | , TestSet8.isTrivalent(new int[] { 22, 19, 10, 10, 19, 22, 22, 10 })); 83 | 84 | Console.Write("isTrivalent of {1, 2, 2, 2, 2, 2, 2} is"); Console.WriteLine(" = {0}" 85 | , TestSet8.isTrivalent(new int[] { 1, 2, 2, 2, 2, 2, 2 })); 86 | 87 | Console.Write("isTrivalent of {2, 2, 3, 3, 3, 3, 2, 41, 65} is"); Console.WriteLine(" = {0}" 88 | , TestSet8.isTrivalent(new int[] { 2, 2, 3, 3, 3, 3, 2, 41, 65 })); 89 | 90 | Console.Write("isTrivalent of {-1, 0, 1, 0, 0, 0} is"); Console.WriteLine(" = {0}" 91 | , TestSet8.isTrivalent(new int[] { -1, 0, 1, 0, 0, 0 })); 92 | 93 | Console.Write("isTrivalent of { 2147483647, -1, -1 ,- 2147483648} is"); Console.WriteLine(" = {0}" 94 | , TestSet8.isTrivalent(new int[] { 2147483647, -1, -1, -2147483648 })); 95 | 96 | Console.Write("isTrivalent of {} is"); Console.WriteLine(" = {0}" 97 | , TestSet8.isTrivalent(new int[] { })); 98 | 99 | Console.WriteLine("========================"); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /MUMSPT/TestSet30/TestSet30.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet30 8 | { 9 | public static class TestSet30 10 | { 11 | 12 | 13 | /// 14 | /// 15 | /// 16 | /// 17 | /// 18 | public static int isMeera(int[] a) 19 | { 20 | int isMeera = 0; 21 | 22 | int isPrime = -1, isZero = -1; 23 | 24 | for (int i = 0; i < a.Length && isMeera ==0; i++) 25 | { 26 | if (Helper.isPrime(a[i])) isPrime = 1; 27 | if (a[i] == 0) isZero = 1; 28 | 29 | if ((isZero == 1 && isPrime == 1)) 30 | isMeera = 1; 31 | } 32 | 33 | if (isPrime == -1 && isZero == -1) 34 | isMeera = 1; 35 | 36 | return isMeera; 37 | } 38 | 39 | public static int isBean(int[] a) 40 | { 41 | int isBean = 1; 42 | 43 | for (int i = 0; i < a.Length && isBean==1; i++) 44 | { 45 | int isValid = 0; 46 | for (int j = 0; j < a.Length; j++) 47 | { 48 | if (a[i] == a[j] + 1 || a[i] == a[j] - 1) 49 | { 50 | isValid = 1; 51 | break; 52 | } 53 | } 54 | 55 | if (isValid == 0) 56 | isBean = 0; 57 | } 58 | 59 | return isBean; 60 | } 61 | 62 | public static int[] fill(int[] arr, int k, int n) 63 | { 64 | if (k < 1 || n < 1) return null; 65 | int index = 0; 66 | int[] arr2 = new int[n]; 67 | for (int i = 0; i < n; i++) 68 | { 69 | index = i % k; 70 | arr2[i] = arr[index]; 71 | } 72 | 73 | return arr2; 74 | } 75 | 76 | public static Boolean sumIsPower(int[] arr) 77 | { 78 | 79 | 80 | int sum = 0, power = 2; 81 | 82 | for (int i = 0; i < arr.Length; i++) 83 | { 84 | sum += arr[i]; 85 | power *= 2; 86 | } 87 | 88 | 89 | 90 | 91 | return (sum == power); 92 | } 93 | 94 | public static int isHollow(int[] a) 95 | { 96 | int isHollow = 1; 97 | 98 | int precdingzero = 0, midzero = 0, followingzero = 0, current = 0; 99 | 100 | 101 | 102 | for (int i = 0; i < a.Length && isHollow==1; i++) 103 | { 104 | current = a[i]; 105 | // 106 | if (current != 0) 107 | { 108 | if (precdingzero != 0 && midzero != 0) 109 | { 110 | followingzero++; 111 | } 112 | else if (midzero == 0 && followingzero == 0) 113 | { 114 | precdingzero++; 115 | } 116 | } 117 | else 118 | { 119 | if (precdingzero != 0 && followingzero != 0) 120 | isHollow = 0; 121 | else if (precdingzero == 0 && followingzero == 0) 122 | { 123 | 124 | midzero++; 125 | } 126 | else if (precdingzero != 0 && followingzero == 0 ) 127 | { 128 | midzero++; 129 | } 130 | } 131 | } 132 | 133 | if (precdingzero != followingzero || midzero < 3) 134 | isHollow = 0; 135 | 136 | return isHollow; 137 | } 138 | 139 | public static int isBean913(int[] a) 140 | { 141 | int isBean = 1; 142 | int has9 = 0, has13 = 0, has7 = 0, has16 = 0; 143 | for (int i = 0; i < a.Length; i++) 144 | { 145 | if (a[i] == 9) 146 | has9 = 1; 147 | if (a[i] == 13) 148 | has13 = 1; 149 | 150 | if (a[i] == 7) 151 | has7 = 1; 152 | if (a[i] == 16) 153 | { 154 | has16 = 1; 155 | if (has7 == 1) break; 156 | } 157 | } 158 | 159 | if ((has9 == 1 && has13 == 0) || (has7 == 1 && has16 == 1)) 160 | isBean = 0; 161 | return isBean; 162 | } 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /MUMSPT/TestSet13/TestSet13Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet13 8 | { 9 | public static class TestSet13Test 10 | { 11 | public static void countRepresentations() 12 | { 13 | Console.WriteLine("test for countRepresentations "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("countRepresentations of 12 is"); Console.WriteLine(" = {0}" 17 | , TestSet13.countRepresentations(12)); 18 | 19 | Console.Write("countRepresentations of 50 is"); Console.WriteLine(" = {0}" 20 | , TestSet13.countRepresentations(50)); 21 | 22 | Console.Write("countRepresentations of 10 is"); Console.WriteLine(" = {0}" 23 | , TestSet13.countRepresentations(10)); 24 | 25 | Console.Write("countRepresentations of 5 is"); Console.WriteLine(" = {0}" 26 | , TestSet13.countRepresentations(5)); 27 | 28 | Console.Write("countRepresentations of 2 is"); Console.WriteLine(" = {0}" 29 | , TestSet13.countRepresentations(2)); 30 | 31 | Console.WriteLine("========================"); 32 | } 33 | 34 | public static void isSequentiallyBounded() 35 | { 36 | Console.WriteLine("test for isSequentiallyBounded "); 37 | Console.WriteLine("========================"); 38 | 39 | Console.Write("isSequentiallyBounded of {2, 3, 3, 99, 99, 99, 99, 99} is"); Console.WriteLine(" = {0}" 40 | , TestSet13.isSequentiallyBounded(new int[] { 2, 3, 3, 99, 99, 99, 99, 99 })); 41 | 42 | Console.Write("isSequentiallyBounded of {1, 2, 3} is"); Console.WriteLine(" = {0}" 43 | , TestSet13.isSequentiallyBounded(new int[] { 1, 2, 3 })); 44 | 45 | Console.Write("isSequentiallyBounded of {2, 3, 3, 3, 3} is"); Console.WriteLine(" = {0}" 46 | , TestSet13.isSequentiallyBounded(new int[] { 2, 3, 3, 3, 3 })); 47 | 48 | Console.Write("isSequentiallyBounded of {12, 12, 9} is"); Console.WriteLine(" = {0}" 49 | , TestSet13.isSequentiallyBounded(new int[] { 12, 12, 9 })); 50 | 51 | Console.Write("isSequentiallyBounded of {0, 1} is"); Console.WriteLine(" = {0}" 52 | , TestSet13.isSequentiallyBounded(new int[] { 0, 1 })); 53 | 54 | Console.Write("isSequentiallyBounded of {-1, 2} is"); Console.WriteLine(" = {0}" 55 | , TestSet13.isSequentiallyBounded(new int[] { -1, 2 })); 56 | 57 | Console.Write("isSequentiallyBounded of {} is"); Console.WriteLine(" = {0}" 58 | , TestSet13.isSequentiallyBounded(new int[] { })); 59 | 60 | Console.Write("isSequentiallyBounded of {5, 5, 5, 5} is"); Console.WriteLine(" = {0}" 61 | , TestSet13.isSequentiallyBounded(new int[] { 5, 5, 5, 5 })); 62 | 63 | Console.Write("isSequentiallyBounded of {5, 5, 5, 2, 5} is"); Console.WriteLine(" = {0}" 64 | , TestSet13.isSequentiallyBounded(new int[] { 5, 5, 5, 2, 5 })); 65 | 66 | 67 | Console.WriteLine("========================"); 68 | } 69 | 70 | public static void isMinMaxDisjoint() 71 | { 72 | Console.WriteLine("test for isMinMaxDisjoint "); 73 | Console.WriteLine("========================"); 74 | 75 | Console.Write("isMinMaxDisjoint of {5, 4, 1, 3, 2} is"); Console.WriteLine(" = {0}" 76 | , TestSet13.isMinMaxDisjoint(new int[] { 5, 4, 1, 3, 2 })); 77 | 78 | Console.Write("isMinMaxDisjoint of {18, -1, 3, 4, 0} is"); Console.WriteLine(" = {0}" 79 | , TestSet13.isMinMaxDisjoint(new int[] { 18, -1, 3, 4, 0 })); 80 | 81 | Console.Write("isMinMaxDisjoint of {9, 0, 5, 9} is"); Console.WriteLine(" = {0}" 82 | , TestSet13.isMinMaxDisjoint(new int[] { 9, 0, 5, 9 })); 83 | 84 | Console.Write("isMinMaxDisjoint of {0, 5, 18, 0, 9} is"); Console.WriteLine(" = {0}" 85 | , TestSet13.isMinMaxDisjoint(new int[] { 0, 5, 18, 0, 9 })); 86 | 87 | Console.Write("isMinMaxDisjoint of {7, 7, 7, 7} is"); Console.WriteLine(" = {0}" 88 | , TestSet13.isMinMaxDisjoint(new int[] { 7, 7, 7, 7 })); 89 | 90 | Console.Write("isMinMaxDisjoint of {} is"); Console.WriteLine(" = {0}" 91 | , TestSet13.isMinMaxDisjoint(new int[] { })); 92 | 93 | Console.Write("isMinMaxDisjoint of {1, 2} is"); Console.WriteLine(" = {0}" 94 | , TestSet13.isMinMaxDisjoint(new int[] { 1, 2 })); 95 | 96 | Console.Write("isMinMaxDisjoint of {1} is"); Console.WriteLine(" = {0}" 97 | , TestSet13.isMinMaxDisjoint(new int[] { 1 })); 98 | 99 | 100 | Console.WriteLine("========================"); 101 | } 102 | 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /MUMSPT/TestSet1/TestSet1Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet1 8 | { 9 | public static class TestSet1Test 10 | { 11 | public static void primeCount() 12 | { 13 | int[] Start = new int[] {10,11,20,1,5,6,-10 }; 14 | int[] End = new int[] { 30,29,22,1,5,2,6}; 15 | for (int i = 0; i < Start.Length; i++) 16 | { 17 | Console.WriteLine("primeCount between {0} and {1} is {2} " 18 | , Start[i], End[i] 19 | , TestSet1.primeCount(Start[i], End[i])); 20 | } 21 | 22 | Console.WriteLine("========================"); 23 | 24 | } 25 | 26 | public static void Fibb() 27 | { 28 | Console.Write("array {2, 1, 1} is"); Console.WriteLine(" = {0}" 29 | , TestSet1.nonRecusiveFib(9)); 30 | string vva = TestSet1.RecusiveFib(9).ToString(); 31 | 32 | Console.Write("array {2, 1, 1} is"); Console.WriteLine(" = {0}" 33 | , vva); 34 | 35 | } 36 | 37 | public static void isMadhavArray() 38 | { 39 | Console.WriteLine("test is isMadhavArray "); 40 | Console.WriteLine("========================"); 41 | Console.Write("array {2, 1, 1} is");Console.WriteLine(" = {0}" 42 | , TestSet1.isMadhavArray(new int[] { 2, 1, 1 })); 43 | 44 | Console.Write("array {2, 1, 1, 4, -1, -1} is"); Console.WriteLine("= {0}" 45 | , TestSet1.isMadhavArray(new int[] { 2, 1, 1, 4, -1, -1 })); 46 | 47 | Console.Write("array {6, 2, 4, 2, 2, 2, 1, 5, 0, 0} is"); Console.WriteLine(" = {0}" 48 | , TestSet1.isMadhavArray(new int[] { 6, 2, 4, 2, 2, 2, 1, 5, 0, 0 })); 49 | 50 | Console.Write("array {18, 9, 10, 6, 6, 6} is"); Console.WriteLine(" = {0}" 51 | , TestSet1.isMadhavArray(new int[] { 18, 9, 10, 6, 6, 6 })); 52 | 53 | Console.Write("array {-6, -3, -3, 8, -5, -4} is"); Console.WriteLine(" = {0}" 54 | , TestSet1.isMadhavArray(new int[] { -6, -3, -3, 8, -5, -4 })); 55 | 56 | Console.Write("array {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, -2, -1} is"); Console.WriteLine(" = {0}" 57 | , TestSet1.isMadhavArray(new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, -2, -1 })); 58 | 59 | Console.Write("array {3, 1, 2, 3, 0} is"); Console.WriteLine(" = {0}" 60 | , TestSet1.isMadhavArray(new int[] { 3, 1, 2, 3, 0 })); 61 | 62 | Console.WriteLine("========================"); 63 | 64 | } 65 | 66 | public static void isInertial() 67 | { 68 | Console.WriteLine("test is isInertial "); 69 | Console.WriteLine("========================"); 70 | Console.Write("array {11, 4, 20, 9, 2, 8} is"); Console.WriteLine(" = {0}" 71 | , TestSet1.isInertial(new int[] { 11, 4, 20, 9, 2, 8 })); 72 | 73 | Console.Write("array {12, 11, 4, 9, 2, 3, 10} is"); Console.WriteLine("= {0}" 74 | , TestSet1.isInertial(new int[] { 12, 11, 4, 9, 2, 3, 10 })); 75 | 76 | Console.Write("array {1} is"); Console.WriteLine(" = {0}" 77 | , TestSet1.isInertial(new int[] { 1 })); 78 | 79 | Console.Write("array {2} is"); Console.WriteLine(" = {0}" 80 | , TestSet1.isInertial(new int[] { 2 })); 81 | 82 | Console.Write("array {1, 2, 3, 4} is"); Console.WriteLine(" = {0}" 83 | , TestSet1.isInertial(new int[] { 1, 2, 3, 4 })); 84 | 85 | Console.Write("array {1, 1, 1, 1, 1, 1, 2} is"); Console.WriteLine(" = {0}" 86 | , TestSet1.isInertial(new int[] { 1, 1, 1, 1, 1, 1, 2 })); 87 | 88 | Console.Write("array {2, 12, 4, 6, 8, 11} is"); Console.WriteLine(" = {0}" 89 | , TestSet1.isInertial(new int[] { 2, 12, 4, 6, 8, 11 })); 90 | 91 | Console.Write("array {2, 12, 12, 4, 6, 8, 11} is"); Console.WriteLine(" = {0}" 92 | , TestSet1.isInertial(new int[] { 2, 12, 12, 4, 6, 8, 11 })); 93 | 94 | Console.Write("array {-2, -4, -6, -8, -11} is"); Console.WriteLine(" = {0}" 95 | , TestSet1.isInertial(new int[] { -2, -4, -6, -8, -11 })); 96 | 97 | Console.Write("array {2, 3, 5, 7} is"); Console.WriteLine(" = {0}" 98 | , TestSet1.isInertial(new int[] { 2, 3, 5, 7 })); 99 | 100 | Console.Write("array {2, 4, 6, 8, 10} is"); Console.WriteLine(" = {0}" 101 | , TestSet1.isInertial(new int[] { 2, 4, 6, 8, 10 })); 102 | 103 | Console.Write("array {} is"); Console.WriteLine(" = {0}" 104 | , TestSet1.isInertial(new int[] { })); 105 | 106 | Console.WriteLine("========================"); 107 | 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /MUMSPT/TestSet24/TestSet24.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet24 8 | { 9 | public static class TestSet24 10 | { 11 | /// 12 | /// A number n is triangular if n == 1 + 2 +…+j for some j. Write a function 13 | /// int isTriangular(int n) 14 | /// that returns 1 if n is a triangular number, otherwise it returns 0. 15 | /// The first 4 triangular numbers are 1 (j=1), 3 (j=2), 6, (j=3), 10 (j=4). 16 | /// 17 | /// 18 | /// 19 | public static int isTriangular(int n) 20 | { 21 | int isTriangular = 0; 22 | 23 | 24 | int sum = 0; 25 | 26 | for (int j = 0; j <= n && isTriangular==0; j++) 27 | { 28 | sum += j; 29 | if(sum == n) 30 | { 31 | isTriangular = 1; 32 | } 33 | } 34 | 35 | return isTriangular; 36 | } 37 | 38 | /// 39 | /// Define an array to be a Mercurial array 40 | /// if a 3 does not occur between any two 1s. 41 | /// Write a function named isMercurial that 42 | /// returns 1 if its array argument is a 43 | /// Mercurial array, otherwise it returns 0 44 | /// 45 | /// 46 | /// 47 | public static int isMercurial(int[] a) 48 | { 49 | int isMercurial = 1; 50 | 51 | int firstone = -1, secondone = -1, firstthree = -1; 52 | 53 | for (int i = 0; i < a.Length && isMercurial == 1; i++) 54 | { 55 | int current = a[i]; 56 | if (current == 1) 57 | { 58 | if (firstthree == -1) 59 | { 60 | firstone = i; 61 | } 62 | else 63 | { 64 | if (firstthree != -1) 65 | { 66 | secondone = i; 67 | if (firstthree != -1 && firstone != -1 && secondone != -1) 68 | { 69 | if (firstone < firstthree && firstthree < secondone) 70 | { 71 | isMercurial = 0; 72 | } 73 | } 74 | } 75 | } 76 | } 77 | else if (current == 3) 78 | if (firstone != -1) 79 | firstthree = i; 80 | } 81 | 82 | if (firstthree != -1 && firstone != -1 && secondone != -1) 83 | { 84 | if (firstone < firstthree && firstthree < secondone) 85 | { 86 | isMercurial = 0; 87 | } 88 | } 89 | 90 | return isMercurial; 91 | } 92 | 93 | /// 94 | /// An array is defined to be a 235 array 95 | /// if the number of elements divisible by 2 96 | /// plus the number of elements divisible by 3 97 | /// plus the number of elements divisible by 5 98 | /// plus the number of elements not divisible by 2, 3, 99 | /// or 5 is equal to the number of elements of the array. 100 | /// Write a method named is123Array that returns 101 | /// 1 if its array argument is a 235 array, otherwise it returns 0. 102 | /// 103 | /// 104 | /// 105 | public static int is235Array(int[] a) 106 | { 107 | int is235Array = 0; 108 | int div2count = 0, div3count = 0, div5count = 0, othercount = 0; 109 | for (int i = 0; i < a.Length; i++) 110 | { 111 | bool isother = true; 112 | int current = a[i]; 113 | if (Helper.IsdivisibleBy(current, 2)) 114 | { 115 | div2count++; 116 | isother = false; 117 | } 118 | if (Helper.IsdivisibleBy(current, 3)) 119 | { 120 | div3count++; 121 | isother = false; 122 | } 123 | if (Helper.IsdivisibleBy(current, 5)) 124 | { 125 | div5count++; 126 | isother = false; 127 | } 128 | if (isother) 129 | othercount++; 130 | 131 | } 132 | 133 | if (div2count + div3count + div5count + othercount == a.Length) 134 | is235Array = 1; 135 | return is235Array; 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /MUMSPT/TestSet26/TestSet26Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet26 8 | { 9 | public static class TestSet26Test 10 | { 11 | public static void isNPrimeable() 12 | { 13 | Console.WriteLine("test for isNPrimeable "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("isNPrimeable of {5, 15, 27} and 2 is"); Console.WriteLine(" = {0}" 17 | , TestSet26.isNPrimeable(new int[] { 5, 15, 27 }, 2)); 18 | 19 | Console.Write("isNPrimeable of {5, 15, 27} and 3 is"); Console.WriteLine(" = {0}" 20 | , TestSet26.isNPrimeable(new int[] { 5, 15, 27 }, 3)); 21 | 22 | Console.Write("isNPrimeable of {5, 15, 26} and 2 is"); Console.WriteLine(" = {0}" 23 | , TestSet26.isNPrimeable(new int[] { 5, 15, 26 }, 2)); 24 | 25 | Console.Write("isNPrimeable of {1, 1, 1, 1, 1, 1, 1} and 4 is"); Console.WriteLine(" = {0}" 26 | , TestSet26.isNPrimeable(new int[] { 1, 1, 1, 1, 1, 1, 1 }, 4)); 27 | 28 | Console.Write("isNPrimeable of {} and 2 is"); Console.WriteLine(" = {0}" 29 | , TestSet26.isNPrimeable(new int[] { }, 2)); 30 | 31 | Console.WriteLine("========================"); 32 | 33 | } 34 | 35 | public static void pairwiseSum() 36 | { 37 | Console.WriteLine("test for computeHMS "); 38 | Console.WriteLine("========================"); 39 | 40 | string ss = ""; 41 | 42 | ss = "{"; 43 | var output = TestSet26.pairwiseSum(new int[] { 2, 1, 18, -5 }); 44 | if (output != null) 45 | { 46 | for (int i = 0; i < output.Length; i++) 47 | { 48 | ss += output[i]; 49 | if (i < output.Length - 1) ss += ","; 50 | 51 | } 52 | ss += "}"; 53 | } 54 | else 55 | { 56 | ss = "NULL"; 57 | } 58 | Console.Write("pairwiseSum of {2, 1, 18, -5} is"); Console.WriteLine(" = {0}" 59 | , ss); 60 | 61 | 62 | /********************************************************/ 63 | 64 | // 65 | ss = "{"; 66 | output = TestSet26.pairwiseSum(new int[] { 2, 1, 18, -5, -5, -15, 0, 0, 1, -1 }); 67 | if (output != null) 68 | { 69 | for (int i = 0; i < output.Length; i++) 70 | { 71 | ss += output[i]; 72 | if (i < output.Length - 1) ss += ","; 73 | 74 | } 75 | ss += "}"; 76 | } 77 | else 78 | { 79 | ss = "NULL"; 80 | } 81 | Console.Write("pairwiseSum of {2, 1, 18, -5, -5, -15, 0, 0, 1, -1} is"); Console.WriteLine(" = {0}" 82 | , ss); 83 | 84 | /********************************************************/ 85 | 86 | /********************************************************/ 87 | 88 | // 89 | ss = "{"; 90 | output = TestSet26.pairwiseSum(new int[] { 2, 1, 18 }); 91 | if (output != null) 92 | { 93 | for (int i = 0; i < output.Length; i++) 94 | { 95 | ss += output[i]; 96 | if (i < output.Length - 1) ss += ","; 97 | 98 | } 99 | ss += "}"; 100 | } 101 | else 102 | { 103 | ss = "NULL"; 104 | } 105 | Console.Write("pairwiseSum of {2, 1, 18} is"); Console.WriteLine(" = {0}" 106 | , ss); 107 | 108 | /********************************************************/ 109 | 110 | /********************************************************/ 111 | 112 | // 113 | ss = "{"; 114 | output = TestSet26.pairwiseSum(new int[] { }); 115 | if (output != null) 116 | { 117 | for (int i = 0; i < output.Length; i++) 118 | { 119 | ss += output[i]; 120 | if (i < output.Length - 1) ss += ","; 121 | 122 | } 123 | ss += "}"; 124 | } 125 | else 126 | { 127 | ss = "NULL"; 128 | } 129 | Console.Write("pairwiseSum of { } is"); Console.WriteLine(" = {0}" 130 | , ss); 131 | 132 | /********************************************************/ 133 | 134 | /********************************************************/ 135 | } 136 | 137 | public static void is121Array() 138 | { 139 | TestSet15.TestSet15Test.is121Array(); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /MUMSPT/TestSet9/TestSet9.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet9 8 | { 9 | public static class TestSet9 10 | { 11 | /// 12 | /// Write a function named equivalentArrays that has two array 13 | /// arguments and returns 1 if the two arrays contain the same 14 | /// values (but not necessarily in the same order), otherwise it returns 0. 15 | /// Your solution must not sort either array or a copy of either array! 16 | /// Also you must not modify either array, i.e., the values in the arrays 17 | /// upon return from the function must be the same as when the function 18 | /// was called. Note that the arrays do not have to have the same number 19 | /// of elements, they just have to have one of more copies of the same values. 20 | /// Hint: If your solution compares the length of the first array with 21 | /// the length of the second array or vice versa, you have misunderstood 22 | /// the problem!! Your solution does not need to determine which array is bigger! 23 | /// 24 | /// 25 | /// 26 | /// 27 | public static int equivalentArrays(int[] a1, int[] a2) 28 | { 29 | int isEquivalent = 1; 30 | 31 | for (int i = 0; i < a1.Length && isEquivalent==1; i++) 32 | { 33 | int exist = 0; 34 | for (int j = 0; j < a2.Length && exist == 0; j++) 35 | { 36 | if (a1[i] == a2[j]) exist = 1; 37 | } 38 | if (exist == 0) isEquivalent = 0; 39 | } 40 | 41 | for (int i = 0; i < a2.Length && isEquivalent == 1; i++) 42 | { 43 | int exist = 0; 44 | for (int j = 0; j < a1.Length && exist == 0; j++) 45 | { 46 | if (a2[i] == a1[j]) exist = 1; 47 | } 48 | if (exist == 0) isEquivalent = 0; 49 | } 50 | 51 | return isEquivalent; 52 | } 53 | 54 | 55 | /// 56 | /// An array is defined to be stepped if it is in ascending order 57 | /// and there are 3 or more occurrences of each distinct value in the array. 58 | /// Note that ascending order means 59 | /// It does not mean a[n] (this is strictly ascending). 60 | /// Write a function named isStepped that returns 1 if its array argument is stepped, 61 | /// otherwise return 0. 62 | /// 63 | /// 64 | /// 65 | public static int isStepped(int[] a) 66 | { 67 | int isStepped = 1; 68 | 69 | int count = 0, current = 0, next = 0; 70 | for (int i = 0; i < a.Length && isStepped==1; i++) 71 | { 72 | int k = (i == a.Length - 1) == true ? i : i + 1; 73 | current = a[i]; 74 | next = a[k]; 75 | //ckeck of is ascending 76 | if (current > next) isStepped = 0; 77 | else 78 | { 79 | if(current == next) 80 | { 81 | count++; 82 | } 83 | else 84 | { 85 | if (current == a[i - 1]) 86 | count++; 87 | if (count < 3) isStepped = 0; 88 | count = 0; 89 | } 90 | } 91 | if (i == a.Length - 1) // check for last item 92 | { 93 | if (count < 3) isStepped = 0; 94 | } 95 | } 96 | 97 | return isStepped; 98 | } 99 | 100 | 101 | /// 102 | /// An array is rapidly increasing if each element (except the first one) is 103 | /// greater than twice the sum of all its preceding elements. 104 | /// Write a method named isRapidlyIncreasing that returns 1 if its array argument is rapidly increasing. 105 | /// Otherwise it returns 0 106 | /// 107 | /// 108 | /// 109 | public static int isRapidlyIncreasing(int[] a) 110 | { 111 | int isRapidlyIncreasing = 1; 112 | 113 | for (int i = a.Length - 1; i > 0 && isRapidlyIncreasing == 1; i--) 114 | { 115 | int sum = 0; 116 | for (int j = i - 1; j >=0 ; j--) 117 | { 118 | sum += a[j]; 119 | } 120 | if (a[i] <= 2 * sum) isRapidlyIncreasing = 0; 121 | } 122 | 123 | return isRapidlyIncreasing; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /MUMSPT/TestSet16/TestSet16Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet16 8 | { 9 | public static class TestSet16Test 10 | { 11 | public static void largestAdjacentSum() 12 | { 13 | Console.WriteLine("test for largestAdjacentSum "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("largestAdjacentSum of {1, 2, 3, 4} is"); Console.WriteLine(" = {0}" 17 | , TestSet16.largestAdjacentSum(new int[] { 1, 2, 3, 4 })); 18 | 19 | Console.Write("largestAdjacentSum of {18, -12, 9, -10} is"); Console.WriteLine(" = {0}" 20 | , TestSet16.largestAdjacentSum(new int[] { 18, -12, 9, -10 })); 21 | 22 | Console.Write("largestAdjacentSum of {1,1,1,1,1,1,1,1,1} is"); Console.WriteLine(" = {0}" 23 | , TestSet16.largestAdjacentSum(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 })); 24 | 25 | Console.Write("largestAdjacentSum of {1,1,1,1,1,2,1,1,1} is"); Console.WriteLine(" = {0}" 26 | , TestSet16.largestAdjacentSum(new int[] { 1, 1, 1, 1, 1, 2, 1, 1, 1 })); 27 | 28 | Console.WriteLine("========================"); 29 | } 30 | 31 | public static void checkConcatenatedSum() 32 | { 33 | Console.WriteLine("test for checkConcatenatedSum "); 34 | Console.WriteLine("========================"); 35 | 36 | Console.Write("checkConcatenatedSum of 192,2 is"); Console.WriteLine(" = {0}" 37 | , TestSet16.checkConcatenatedSum(198,2)); 38 | 39 | Console.Write("checkConcatenatedSum of 192,3 is"); Console.WriteLine(" = {0}" 40 | , TestSet16.checkConcatenatedSum(198, 3)); 41 | 42 | Console.Write("checkConcatenatedSum of 2997,3 is"); Console.WriteLine(" = {0}" 43 | , TestSet16.checkConcatenatedSum(2997, 3)); 44 | 45 | Console.Write("checkConcatenatedSum of 2997,2 is"); Console.WriteLine(" = {0}" 46 | , TestSet16.checkConcatenatedSum(2997, 2)); 47 | 48 | Console.Write("checkConcatenatedSum of 13332,4 is"); Console.WriteLine(" = {0}" 49 | , TestSet16.checkConcatenatedSum(13332, 4)); 50 | 51 | Console.Write("checkConcatenatedSum of 9,1 is"); Console.WriteLine(" = {0}" 52 | , TestSet16.checkConcatenatedSum(9, 1)); 53 | 54 | Console.WriteLine("========================"); 55 | } 56 | 57 | public static void isSequencedArray() 58 | { 59 | Console.WriteLine("test for isSequencedArray "); 60 | Console.WriteLine("========================"); 61 | 62 | Console.Write("isSequencedArray of {2, 2, 3, 4, 4, 4, 5} ,2,5 is"); Console.WriteLine(" = {0}" 63 | , TestSet16.isSequencedArray(new int[] { 2, 2, 3, 4, 4, 4, 5 }, 2, 5)); 64 | 65 | Console.Write("isSequencedArray of {2, 2, 3, 5, 5, 5} ,2,5 is"); Console.WriteLine(" = {0}" 66 | , TestSet16.isSequencedArray(new int[] { 2, 2, 3, 5, 5, 5 }, 2, 5)); 67 | 68 | Console.Write("isSequencedArray of {0, 2, 2, 3, 3} ,2,3 is"); Console.WriteLine(" = {0}" 69 | , TestSet16.isSequencedArray(new int[] { 0, 2, 2, 3, 3 }, 2, 3)); 70 | 71 | Console.Write("isSequencedArray of {1,1, 3, 2, 2, 4} ,1,4 is"); Console.WriteLine(" = {0}" 72 | , TestSet16.isSequencedArray(new int[] { 1, 1, 3, 2, 2, 4 }, 1, 4)); 73 | 74 | Console.Write("isSequencedArray of {1, 2, 3, 4, 5} ,1,5 is"); Console.WriteLine(" = {0}" 75 | , TestSet16.isSequencedArray(new int[] { 1, 2, 3, 4, 5 }, 1, 5)); 76 | 77 | Console.Write("isSequencedArray of {1, 3, 4, 2, 5} ,1,5 is"); Console.WriteLine(" = {0}" 78 | , TestSet16.isSequencedArray(new int[] { 1, 3, 4, 2, 5 }, 1, 5)); 79 | 80 | Console.Write("isSequencedArray of {-5, -5, -4, -4, -4, -3, -3, -2, -2, -2} ,-5,-2 is"); Console.WriteLine(" = {0}" 81 | , TestSet16.isSequencedArray(new int[] { -5, -5, -4, -4, -4, -3, -3, -2, -2, -2 }, -5, -2)); 82 | 83 | Console.Write("isSequencedArray of {0, 1, 2, 3, 4, 5} ,1,5 is"); Console.WriteLine(" = {0}" 84 | , TestSet16.isSequencedArray(new int[] { 0, 1, 2, 3, 4, 5 }, 1, 5)); 85 | 86 | Console.Write("isSequencedArray of {1, 2, 3, 4} ,1,5 is"); Console.WriteLine(" = {0}" 87 | , TestSet16.isSequencedArray(new int[] { 1, 2, 3, 4 }, 1, 5)); 88 | 89 | Console.Write("isSequencedArray of {1, 2, 5} ,1,5 is"); Console.WriteLine(" = {0}" 90 | , TestSet16.isSequencedArray(new int[] { 1, 2, 5 }, 1, 5)); 91 | 92 | Console.Write("isSequencedArray of {5, 4, 3, 2, 1} ,1,5 is"); Console.WriteLine(" = {0}" 93 | , TestSet16.isSequencedArray(new int[] { 5, 4, 3, 2, 1 }, 1, 5)); 94 | 95 | Console.WriteLine("========================"); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /MUMSPT/TestSet17/TestSet17.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet17 8 | { 9 | public static class TestSet17 10 | { 11 | 12 | /// 13 | /// Write a function named largestPrimeFactor that will 14 | /// return the largest prime factor of a number. 15 | /// If the number is <=1 it should return 0. 16 | /// Recall that a prime number is a number > 1 that is 17 | /// divisible only by 1 and itself, e.g., 18 | /// 13 is prime but 14 is not. 19 | /// 20 | /// 21 | /// 22 | public static int largestPrimeFactor(int n) 23 | { 24 | int largestpf = 0; 25 | 26 | if (n <= 1) return 0; 27 | for (int i = 2; i <= n; i++) 28 | { 29 | if (n % i == 0) 30 | if (Helper.isPrime(i)) 31 | if (i > largestpf) 32 | largestpf = i; 33 | } 34 | 35 | return largestpf; 36 | } 37 | 38 | 39 | /// 40 | /// The fundamental theorem of arithmetic states that 41 | /// every natural number greater than 1 can be written 42 | /// as a unique product of prime numbers. So, for 43 | /// instance, 6936=2*2*2*3*17*17. Write a method 44 | /// named encodeNumber what will encode a number 45 | /// n as an array that contains the prime numbers that, 46 | /// when multipled together, will equal n. 47 | /// So encodeNumber(6936) would return the array {2, 2, 2, 3, 17, 17}. 48 | /// If the number is <= 1 the function should return null; 49 | /// 50 | /// 51 | /// 52 | public static int[] encodeNumber(int n) 53 | { 54 | if (n < 2) return null; 55 | int primecount = 0; 56 | List temparray = new List(); // temp array if i can to use it 57 | int i = 2; 58 | do 59 | { 60 | if (Helper.isPrime(i)) 61 | { 62 | if (Helper.IsWholeNumber(n / (double)i)) 63 | { 64 | primecount++; 65 | temparray.Add(i); 66 | n = n / i; 67 | } 68 | else 69 | { 70 | i++; 71 | } 72 | } 73 | else 74 | { 75 | i++; 76 | } 77 | } while (n > 1); 78 | 79 | int[] encodearray = new int[primecount]; 80 | for (int j = 0; j < primecount; j++) 81 | { 82 | encodearray[j] = temparray[j]; 83 | } 84 | 85 | return encodearray; 86 | } 87 | 88 | 89 | /// 90 | /// Consider a simple pattern matching language that 91 | /// matches arrays of integers. A pattern is an array 92 | /// of integers. An array matches a pattern if it contains 93 | /// sequences of the pattern elements in the same order 94 | /// as they appear in the pattern. So for example, 95 | /// the array {1, 1, 1, 2, 2, 1, 1, 3} matches the pattern {1, 2, 1, 3} 96 | /// 97 | /// 98 | /// 99 | /// 100 | public static int matchPattern(int[] a, int[] pattern) 101 | { 102 | 103 | // len is the number of elements in the array a, patternLen is the number of elements in the pattern. 104 | int len = a.Length; 105 | int patternLen = pattern.Length; 106 | int i = 0; // index into a 107 | int k = 0; // index into pattern 108 | int matches = 0; // how many times current pattern character has been matched so far 109 | 110 | for (i = 0; i < len; i++) 111 | { 112 | 113 | if (a[i] == pattern[k]) 114 | matches++; // current pattern character was matched 115 | else if (matches == 0 || k == patternLen - 1) 116 | return 0; // if pattern[k] was never matched (matches==0) or at end of pattern (k==patternLen-1) 117 | else 118 | { 119 | // advance to next pattern character 120 | // !!You write this code!! 121 | k++; 122 | if (a[i] == pattern[k]) 123 | matches++; 124 | else 125 | return 0; 126 | 127 | 128 | } // end of else 129 | 130 | } // end of for 131 | 132 | // return 1 if at end of array a (i==len) and also at end of pattern (k==patternLen-1) 133 | 134 | if (i == len && k == patternLen - 1) return 1; else return 0; 135 | 136 | } 137 | 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /MUMSPT/TestSet14/TestSet14Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet14 8 | { 9 | public static class TestSet14Test 10 | { 11 | public static void fullnessQuotient() 12 | { 13 | Console.WriteLine("test for fullnessQuotient "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("fullnessQuotient of 94 is"); Console.WriteLine(" = {0}" 17 | , TestSet14.fullnessQuotient(94)); 18 | 19 | Console.Write("fullnessQuotient of 1 is"); Console.WriteLine(" = {0}" 20 | , TestSet14.fullnessQuotient(1)); 21 | 22 | Console.Write("fullnessQuotient of 9 is"); Console.WriteLine(" = {0}" 23 | , TestSet14.fullnessQuotient(9)); 24 | 25 | Console.Write("fullnessQuotient of 360 is"); Console.WriteLine(" = {0}" 26 | , TestSet14.fullnessQuotient(360)); 27 | 28 | Console.Write("fullnessQuotient of -4 is"); Console.WriteLine(" = {0}" 29 | , TestSet14.fullnessQuotient(-4)); 30 | 31 | 32 | Console.WriteLine("========================"); 33 | } 34 | 35 | public static void isPacked() 36 | { 37 | Console.WriteLine("test for isPacked "); 38 | Console.WriteLine("========================"); 39 | 40 | Console.Write("isPacked of {2, 2, 3, 3, 3} is"); Console.WriteLine(" = {0}" 41 | , TestSet14.isPacked(new int[] { 2, 2, 3, 3, 3 })); 42 | 43 | Console.Write("isPacked of {2, 3, 2, 3, 3} is"); Console.WriteLine(" = {0}" 44 | , TestSet14.isPacked(new int[] { 2, 3, 2, 3, 3 })); 45 | 46 | Console.Write("isPacked of {2, 2, 2, 3, 3, 3} is"); Console.WriteLine(" = {0}" 47 | , TestSet14.isPacked(new int[] { 2, 2, 2, 3, 3, 3 })); 48 | 49 | Console.Write("isPacked of {2, 2, 1} is"); Console.WriteLine(" = {0}" 50 | , TestSet14.isPacked(new int[] { 2, 2, 1 })); 51 | 52 | Console.Write("isPacked of {2, 2, 1, 2, 2} is"); Console.WriteLine(" = {0}" 53 | , TestSet14.isPacked(new int[] { 2, 2, 1, 2, 2 })); 54 | 55 | Console.Write("isPacked of {4, 4, 4, 4, 1, 2, 2, 3, 3, 3} is"); Console.WriteLine(" = {0}" 56 | , TestSet14.isPacked(new int[] { 4, 4, 4, 4, 1, 2, 2, 3, 3, 3 } )); 57 | 58 | Console.Write("isPacked of {7, 7, 7, 7, 7, 7, 7, 1} is"); Console.WriteLine(" = {0}" 59 | , TestSet14.isPacked(new int[] { 7, 7, 7, 7, 7, 7, 7, 1 })); 60 | 61 | Console.Write("isPacked of {7, 7, 7, 7, 1, 7, 7, 7} is"); Console.WriteLine(" = {0}" 62 | , TestSet14.isPacked(new int[] { 7, 7, 7, 7, 1, 7, 7, 7 })); 63 | 64 | Console.Write("isPacked of {7, 7, 7, 7, 7, 7, 7} is"); Console.WriteLine(" = {0}" 65 | , TestSet14.isPacked(new int[] { 7, 7, 7, 7, 7, 7, 7 })); 66 | 67 | Console.Write("isPacked of {} is"); Console.WriteLine(" = {0}" 68 | , TestSet14.isPacked(new int[] { })); 69 | 70 | 71 | Console.Write("isPacked of {1, 2, 1} is"); Console.WriteLine(" = {0}" 72 | , TestSet14.isPacked(new int[] { 1, 2, 1 })); 73 | 74 | Console.Write("isPacked of {2, 1, 1} is"); Console.WriteLine(" = {0}" 75 | , TestSet14.isPacked(new int[] { 2, 1, 1 })); 76 | 77 | Console.Write("isPacked of {-3, -3, -3} is"); Console.WriteLine(" = {0}" 78 | , TestSet14.isPacked(new int[] { -3, -3, -3 })); 79 | 80 | Console.Write("isPacked of {0, 2, 2} is"); Console.WriteLine(" = {0}" 81 | , TestSet14.isPacked(new int[] { 0, 2, 2 })); 82 | 83 | Console.Write("isPacked of {2, 1, 2} is"); Console.WriteLine(" = {0}" 84 | , TestSet14.isPacked(new int[] { 2, 1, 2 })); 85 | 86 | Console.WriteLine("========================"); 87 | } 88 | 89 | 90 | 91 | public static void isOddHeavy() 92 | { 93 | Console.WriteLine("test for isOddHeavy "); 94 | Console.WriteLine("========================"); 95 | 96 | Console.Write("isOddHeavy of {11, 4, 9, 2, 8} is"); Console.WriteLine(" = {0}" 97 | , TestSet14.isOddHeavy(new int[] { 11, 4, 9, 2, 8 })); 98 | 99 | Console.Write("isOddHeavy of {11, 4, 9, 2, 3, 10} is"); Console.WriteLine(" = {0}" 100 | , TestSet14.isOddHeavy(new int[] { 11, 4, 9, 2, 3, 10 })); 101 | 102 | Console.Write("isOddHeavy of {1} is"); Console.WriteLine(" = {0}" 103 | , TestSet14.isOddHeavy(new int[] { 1 })); 104 | 105 | Console.Write("isOddHeavy of {2} is"); Console.WriteLine(" = {0}" 106 | , TestSet14.isOddHeavy(new int[] { 2 })); 107 | 108 | Console.Write("isOddHeavy of {1, 1, 1, 1, 1, 1} is"); Console.WriteLine(" = {0}" 109 | , TestSet14.isOddHeavy(new int[] { 1, 1, 1, 1, 1, 1 })); 110 | 111 | Console.Write("isOddHeavy of {2, 4, 6, 8, 11} is"); Console.WriteLine(" = {0}" 112 | , TestSet14.isOddHeavy(new int[] { 2, 4, 6, 8, 11 })); 113 | 114 | Console.Write("isOddHeavy of {-2, -4, -6, -8, -11} is"); Console.WriteLine(" = {0}" 115 | , TestSet14.isOddHeavy(new int[] { -2, -4, -6, -8, -11 })); 116 | 117 | Console.WriteLine("========================"); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /MUMSPT/TestSet15/TestSet15.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet15 8 | { 9 | public static class TestSet15 10 | { 11 | /// 12 | /// Write a method named getExponent(n, p) that returns the largest 13 | /// exponent x such that px evenly divides n. If p is <= 1 the method should return -1. 14 | /// For example, getExponent(162, 3) returns 4 because 162 = 21 * 34, 15 | /// therefore the value of x here is 4. 16 | /// 17 | /// 18 | /// 19 | /// 20 | public static int getExponent(int n, int p) 21 | { 22 | if (p <= 1) return -1; 23 | bool isok = false; 24 | int x = 0; 25 | double temp = n; 26 | do 27 | { 28 | 29 | temp = temp / p; 30 | isok=(temp % 1 == 0); 31 | if (isok) 32 | { 33 | x++; 34 | } 35 | 36 | } while (isok); 37 | 38 | return x; 39 | } 40 | 41 | /// 42 | /// Define an array to be a 121 array if all 43 | /// its elements are either 1 or 2 and it begins with one or 44 | /// more 1s followed by a one or more 2s and then ends with 45 | /// the same number of 1s that it begins with. Write a method 46 | /// named is121Array that returns 1 if its array argument is a 121 array, 47 | /// otherwise, it returns 0 48 | /// 49 | /// 50 | /// 51 | public static int is121Array(int[] a) 52 | { 53 | int is121 = 1; 54 | int fristonecount = 0, secondonecount=0, twocount = 0; 55 | int prevelement = 0; 56 | int changecount = 0; 57 | int firstelement = a[0]; 58 | if (firstelement != 1) is121 = 0; 59 | for (int i = 0; i < a.Length && is121==1; i++) 60 | { 61 | // if (a[i] != 1 && a[i] != 2) is121 = 0; 62 | if (a[i] == 2) twocount++; 63 | 64 | if (prevelement != a[i]) changecount++; 65 | if(changecount ==1) if (a[i] == 1) fristonecount++; 66 | if(changecount == 3) if (a[i] == 1) secondonecount++; 67 | if (changecount > 3) is121 = 0; 68 | 69 | prevelement = a[i]; 70 | } 71 | if (twocount < 1) is121 = 0; 72 | if (fristonecount != secondonecount) is121 = 0; 73 | return is121; 74 | } 75 | 76 | 77 | /// 78 | /// A binary representation of a number can be used to select elements from an array. For example, 79 | /// n: 88 = 23 + 24 + 26 (1011000) 80 | /// array: 8, 4, 9, 0, 3, 1, 2 81 | /// indexes 0 1 2 3 4 5 6 82 | /// selected * * * 83 | /// result 0, 3, 2 84 | /// so the result of filtering {8, 4, 9, 0, 3, 1, 2} using 88 would be {0, 3, 2} 85 | /// In the above, the elements that are selected are those whose indices are 86 | /// used as exponents in the binary representation of 88. 87 | /// In other words, a[3], a[4], and a[6] are selected for the result 88 | /// because 3, 4 and 6 are the powers of 2 that sum to 88. 89 | /// Write a method named filterArray that takes an array and a 90 | /// non-negative integer and returns the result of filtering 91 | /// the array using the binary representation of the integer. 92 | /// The returned array must big enough to contain the filtered 93 | /// elements and no bigger. So in the above example, the returned 94 | /// array has length of 3, not 7 (which is the size of the original array.) 95 | /// Futhermore, if the input array is not big enough to contain 96 | /// all the selected elements, then the method returns null. 97 | /// For example, if n=3 is used to filter the array a = {18}, 98 | /// the method should return null because 3=20+21 and hence 99 | /// requires that the array have at least 2 elements a[0] and a[1], 100 | /// but there is no a[1]. 101 | /// 102 | /// 103 | /// 104 | /// 105 | public static int[] filterArray(int[] a, int n) 106 | { 107 | int indexcount = 0; 108 | int digitindex = 0; 109 | 110 | int[] temp = new int[a.Length]; 111 | do 112 | { 113 | int ldigit = n % 2; 114 | if(ldigit ==1) 115 | { 116 | temp[indexcount] = digitindex; 117 | indexcount++; 118 | } 119 | n = n / 2; 120 | digitindex++; 121 | } while (n>0); 122 | 123 | int[] filterarray = new int[indexcount]; 124 | for (int i = 0; i < indexcount; i++) 125 | { 126 | int index = temp[i]; 127 | if (index > a.Length-1) return null; 128 | filterarray[i] = a[index]; 129 | 130 | } 131 | 132 | return filterarray; 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /MUMSPT/TestSet9/TestSet9Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet9 8 | { 9 | public static class TestSet9Test 10 | { 11 | public static void equivalentArrays() 12 | { 13 | Console.WriteLine("test for equivalentArrays "); 14 | Console.WriteLine("========================"); 15 | 16 | Console.Write("equivalentArrays of { 0, 1, 2 }, { 2, 0, 1 } is"); Console.WriteLine(" = {0}" 17 | , TestSet9.equivalentArrays(new int[] { 0, 1, 2 }, new int[] { 2, 0, 1 })); 18 | 19 | Console.Write("equivalentArrays of {0, 1, 2, 1}, { 2, 0, 1 } is"); Console.WriteLine(" = {0}" 20 | , TestSet9.equivalentArrays(new int[] { 0, 1, 2, 1 }, new int[] { 2, 0, 1 })); 21 | 22 | Console.Write("equivalentArrays of {2, 0, 1}, {0, 1, 2, 1} is"); Console.WriteLine(" = {0}" 23 | , TestSet9.equivalentArrays(new int[] { 2, 0, 1 }, new int[] { 0, 1, 2, 1 })); 24 | 25 | Console.Write("equivalentArrays of {0, 5, 5, 5, 1, 2, 1}, {5, 2, 0, 1} is"); Console.WriteLine(" = {0}" 26 | , TestSet9.equivalentArrays(new int[] { 0, 5, 5, 5, 1, 2, 1 }, new int[] { 5, 2, 0, 1 })); 27 | 28 | Console.Write("equivalentArrays of {5, 2, 0, 1}, {0, 5, 5, 5, 1, 2, 1} is"); Console.WriteLine(" = {0}" 29 | , TestSet9.equivalentArrays(new int[] { 5, 2, 0, 1 }, new int[] { 0, 5, 5, 5, 1, 2, 1 })); 30 | 31 | Console.Write("equivalentArrays of {0, 2, 1, 2}, {3, 1, 2, 0} is"); Console.WriteLine(" = {0}" 32 | , TestSet9.equivalentArrays(new int[] { 0, 2, 1, 2 }, new int[] { 3, 1, 2, 0 })); 33 | 34 | Console.Write("equivalentArrays of {3, 1, 2, 0}, {0, 2, 1, 0} is"); Console.WriteLine(" = {0}" 35 | , TestSet9.equivalentArrays(new int[] { 3, 1, 2, 0 }, new int[] { 0, 2, 1, 0 })); 36 | 37 | Console.Write("equivalentArrays of {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 2} is"); Console.WriteLine(" = {0}" 38 | , TestSet9.equivalentArrays(new int[] { 1, 1, 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1, 1, 2 })); 39 | 40 | Console.Write("equivalentArrays of {}, {3, 1, 1, 1, 1, 2} is"); Console.WriteLine(" = {0}" 41 | , TestSet9.equivalentArrays(new int[] { }, new int[] { 3, 1, 1, 1, 1, 2 })); 42 | 43 | Console.Write("equivalentArrays of {}, {} is"); Console.WriteLine(" = {0}" 44 | , TestSet9.equivalentArrays(new int[] { }, new int[] { })); 45 | 46 | Console.WriteLine("========================"); 47 | } 48 | 49 | public static void isStepped() 50 | { 51 | Console.WriteLine("test for isStepped "); 52 | Console.WriteLine("========================"); 53 | 54 | Console.Write("isStepped of {1, 1, 1, 5, 5, 5, 5, 8, 8, 8} is"); Console.WriteLine(" = {0}" 55 | , TestSet9.isStepped(new int[] { 1, 1, 1, 5, 5, 5, 5, 8, 8, 8 })); 56 | 57 | Console.Write("isStepped of {1, 1, 5, 5, 5, 5, 8, 8, 8} is"); Console.WriteLine(" = {0}" 58 | , TestSet9.isStepped(new int[] { 1, 1, 5, 5, 5, 5, 8, 8, 8 })); 59 | 60 | Console.Write("isStepped of {5, 5, 5, 15} is"); Console.WriteLine(" = {0}" 61 | , TestSet9.isStepped(new int[] { 5, 5, 5, 15 })); 62 | 63 | Console.Write("isStepped of {3, 3, 3, 2, 2, 2, 5, 5, 5} is"); Console.WriteLine(" = {0}" 64 | , TestSet9.isStepped(new int[] { 3, 3, 3, 2, 2, 2, 5, 5, 5 })); 65 | 66 | Console.Write("isStepped of {3, 3, 3, 2, 2, 2, 1, 1, 1} is"); Console.WriteLine(" = {0}" 67 | , TestSet9.isStepped(new int[] { 3, 3, 3, 2, 2, 2, 1, 1, 1 })); 68 | 69 | Console.Write("isStepped of {1, 1, 1} is"); Console.WriteLine(" = {0}" 70 | , TestSet9.isStepped(new int[] { 1, 1, 1 })); 71 | 72 | Console.Write("isStepped of {1, 1, 1, 1, 1, 1, 1} is"); Console.WriteLine(" = {0}" 73 | , TestSet9.isStepped(new int[] { 1, 1, 1, 1, 1, 1, 1 })); 74 | 75 | Console.WriteLine("========================"); 76 | } 77 | 78 | public static void isRapidlyIncreasing() 79 | { 80 | Console.WriteLine("test for isRapidlyIncreasing "); 81 | Console.WriteLine("========================"); 82 | 83 | Console.Write("isRapidlyIncreasing of {1, 3, 9, 27} is"); Console.WriteLine(" = {0}" 84 | , TestSet9.isRapidlyIncreasing(new int[] { 1, 3, 9, 27 })); 85 | 86 | Console.Write("isRapidlyIncreasing of {1, 3, 200, 500} is"); Console.WriteLine(" = {0}" 87 | , TestSet9.isRapidlyIncreasing(new int[] { 1, 3, 200, 500 })); 88 | 89 | Console.Write("isRapidlyIncreasing of {1} is"); Console.WriteLine(" = {0}" 90 | , TestSet9.isRapidlyIncreasing(new int[] { 1 })); 91 | 92 | Console.Write("isRapidlyIncreasing of {1, 3, 9, 26} is"); Console.WriteLine(" = {0}" 93 | , TestSet9.isRapidlyIncreasing(new int[] { 1, 3, 9, 26 })); 94 | 95 | Console.Write("isRapidlyIncreasing of {1, 3, 7, 26} is"); Console.WriteLine(" = {0}" 96 | , TestSet9.isRapidlyIncreasing(new int[] { 1, 3, 7, 26 })); 97 | 98 | Console.Write("isRapidlyIncreasing of {1, 3, 8, 26} is"); Console.WriteLine(" = {0}" 99 | , TestSet9.isRapidlyIncreasing(new int[] { 1, 3, 8, 26 })); 100 | 101 | 102 | Console.WriteLine("========================"); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /MUMSPT/TestSet8/TestSet8.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet8 8 | { 9 | public static class TestSet8 10 | { 11 | /// 12 | /// Define a positive number to be isolated if none of the digits in 13 | /// its square are in its cube. For example 163 is n isolated number 14 | /// because 69*69 = 26569 and 69*69*69 = 4330747 and the square does 15 | /// not contain any of the digits 0, 3, 4 and 7 which are the digits used 16 | /// in the cube. On the other hand 162 is not an isolated number 17 | /// because 162*162=26244 and 162*162*162 = 4251528 and the digits 2 and 4 18 | /// which appear in the square are also in the cube. 19 | /// Note that the type of the input parameter is long. 20 | /// The maximum positive number that can be represented as a long is 63 bits long. 21 | /// This allows us to test numbers up to 2,097,151 because the cube of 2,097,151 22 | /// can be represented as a long. However, the cube of 2,097,152 requires more 23 | /// than 63 bits to represent it and hence cannot be computed without extra effort. 24 | /// Therefore, your function should test if n is larger than 2,097,151 and return -1 25 | /// if it is. If n is less than 1 your function should also return -1 26 | /// Hint: n % 10 is the rightmost digit of n, n = n/10 shifts the digits of n one place to the right. 27 | /// 28 | /// 29 | /// 30 | public static int isIsolated(long n) 31 | { 32 | int isIsolated = 1; 33 | if (n < 1 || n > 2097151) isIsolated = -1; 34 | 35 | 36 | long cube = n * n * n; 37 | do 38 | { 39 | long lastcubedigit = cube % 10; 40 | cube = cube / 10; 41 | long squar = n * n; 42 | do 43 | { 44 | long lastsquarDigit = squar % 10; 45 | if (lastcubedigit == lastsquarDigit) 46 | isIsolated = 0; 47 | squar = squar / 10; 48 | 49 | } while (isIsolated >= 1 && squar > 1); 50 | 51 | } while (isIsolated >= 1 && cube > 1); 52 | 53 | return isIsolated; 54 | } 55 | 56 | /// 57 | /// An array is called vanilla if all its elements are made up of the same digit. 58 | /// For example {1, 1, 11, 1111, 1111111} is a vanilla array because all its 59 | /// elements use only the digit 1. However, the array {11, 101, 1111, 11111} 60 | /// is not a vanilla array because its elements use the digits 0 and 1. 61 | /// Write a method called isVanilla that returns 1 if its argument is a 62 | /// vanilla array. Otherwise it returns 0. 63 | /// 64 | /// 65 | /// 66 | public static int isVanilla(int[] a) 67 | { 68 | int isvanilla = 1; 69 | // if (a.Length == 0) return isvanilla; 70 | int firstelement = -12345; 71 | 72 | for (int i = 0; i < a.Length && isvanilla == 1; i++) 73 | { 74 | int n = Math.Abs(a[i]); 75 | do 76 | { 77 | int lastdigit = n % 10; 78 | if (firstelement == -12345) 79 | { 80 | firstelement = lastdigit; 81 | } 82 | if (firstelement != lastdigit) 83 | isvanilla = 0; 84 | n = n / 10; 85 | 86 | } while (isvanilla == 1 && n > 1); 87 | } 88 | 89 | return isvanilla; 90 | } 91 | 92 | /// 93 | /// Define an array to be trivalent if all its elements are one of three 94 | /// different values. For example, {22, 19, 10, 10, 19, 22, 22, 10} is 95 | /// trivalent because all elements are either 10, 22, or 19. However, 96 | /// the array {1, 2, 2, 2, 2, 2, 2} is not trivalent because it contains 97 | /// only two different values (1, 2). The array {2, 2, 3, 3, 3, 3, 2, 41, 65} 98 | /// is not trivalent because it contains four different values (2, 3, 41, 65). 99 | /// 100 | /// 101 | /// 102 | public static int isTrivalent(int[] a) 103 | { 104 | int isTrivalent = 1; 105 | 106 | int[] uniq = new int[3]; 107 | int uniqindex = 0; 108 | int isFristInit = 0; 109 | for (int i = 0; i < a.Length && isTrivalent == 1; i++) 110 | { 111 | if (isFristInit == a[i] ) isFristInit = 1; 112 | if (Helper.isExistInArray(uniq, a[i]) == -1 || isFristInit ==1) 113 | { 114 | if (isFristInit ==1) isFristInit = -1; 115 | if (uniqindex > 2) 116 | isTrivalent = 0; 117 | else 118 | { 119 | uniq[uniqindex] = a[i]; 120 | } 121 | uniqindex++; 122 | } 123 | } 124 | 125 | if (uniqindex < 3) 126 | isTrivalent = 0; 127 | 128 | return isTrivalent; 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /MUMSPT/TestSet25/TestSet25.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet25 8 | { 9 | public static class TestSet25 10 | { 11 | /// 12 | /// Write a method named computeHMS 13 | /// that computes the number of hours, 14 | /// minutes and seconds in a given number of seconds. 15 | /// If you are programming in Java or C#, the method signature is 16 | /// int[] computeHMS(int seconds); 17 | /// The returned array has 3 elements; 18 | /// arr[0] is the hours, arr[1] is the minutes and arr[2] 19 | /// is the seconds contained within the seconds argument. 20 | /// 21 | /// 22 | /// 23 | public static int[] computeHMS(int seconds) 24 | { 25 | int secondsData = 3600; 26 | int[] arr = new int[3]; 27 | 28 | for (int i = 0; i < arr.Length; i++) 29 | { 30 | arr[i] = seconds / secondsData; 31 | seconds = seconds % secondsData; 32 | secondsData = secondsData / 60; 33 | } 34 | return arr; 35 | } 36 | 37 | 38 | /// 39 | /// Define an array to be a Martian array if 40 | /// the number of 1s is greater than the 41 | /// number of 2s and no two adjacent elements are equal. 42 | /// Write a function named isMartian that returns 1 if its 43 | /// argument is a Martian array; otherwise it returns 0. 44 | /// There are two additional requirements. 45 | /// 1. You should return 0 as soon as it is 46 | /// known that the array is not a Martian array; 47 | /// continuing to analyze the array would be a waste of CPU cycles. 48 | /// 2. There should be exactly one loop in your solution. 49 | /// Hint: Make sure that your solution does not exceed the boundaries of the array! 50 | /// 51 | /// 52 | /// 53 | public static int isMartian(int[] a) 54 | { 55 | int isMartian = 1; 56 | int oneCount = 0, TwoCount = 0; 57 | for (int i = 0, j = a.Length - 1; i < j && isMartian == 1; i++, j--) 58 | { 59 | if (a[i] == 1) 60 | oneCount++; 61 | else if (a[i] == 2) 62 | TwoCount++; 63 | if (a[j] == 1) 64 | oneCount++; 65 | else if (a[j] == 2) 66 | TwoCount++; 67 | 68 | 69 | // check for adjacent elements are equal 70 | if (a[i] == a[i + 1] || a[j] == a[j - 1]) 71 | { 72 | isMartian = 0; 73 | break; 74 | } 75 | 76 | 77 | // if array length event 78 | if (j - i == 1) 79 | { 80 | if (oneCount <= TwoCount) isMartian = 0; 81 | } 82 | 83 | // if array length odd 84 | if (j - i == 2) 85 | { 86 | // last element the middle of the array 87 | i++; 88 | 89 | if (a[i] == 1) 90 | oneCount++; 91 | else if (a[i] == 2) 92 | TwoCount++; 93 | 94 | if (oneCount <= TwoCount) isMartian = 0; 95 | } 96 | } 97 | 98 | return isMartian; 99 | 100 | } 101 | 102 | /// 103 | /// An array is defined to be paired-N if 104 | /// it contains two distinct elements that 105 | /// sum to N for some specified value of 106 | /// N and the indexes of those elements 107 | /// also sum to N. Write a function named 108 | /// isPairedN that returns 1 if its array 109 | /// parameter is a paired-N array, 110 | /// otherwise it returns 0. 111 | /// The value of N is passed as the second parameter. 112 | /// There are two additional requirements. 113 | /// 1. Once you know the array is paired-N, 114 | /// you should return 1. No wasted loop iterations please. 115 | /// 2. Do not enter the loop unless you have to. 116 | /// You should test the length of the array and the 117 | /// value of n to determine whether the array 118 | /// could possibly be a paired-N array. 119 | /// If the tests indicate no, 120 | /// return 0 before entering the loop. 121 | /// 122 | /// 123 | /// 124 | /// 125 | public static int isPairedN(int[] a, int n) 126 | { 127 | int isPairedN = 0; 128 | 129 | if (a.Length - 1 + a.Length - 2 < n || n < 0) 130 | { 131 | return 0; 132 | } 133 | 134 | for (int i = 0; i < a.Length && isPairedN==0; i++) 135 | { 136 | for (int j = i; j < a.Length && isPairedN == 0; j++) 137 | { 138 | if(a[i] +a[j] == n) 139 | { 140 | if (i + j == n) 141 | isPairedN = 1; 142 | } 143 | } 144 | } 145 | 146 | return isPairedN; 147 | } 148 | 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /MUMSPT/TestSet19/TestSet19.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet19 8 | { 9 | public static class TestSet19 10 | { 11 | /// 12 | /// An array is zero-plentiful if it 13 | /// contains at least one 0 and every 14 | /// sequence of 0s is of length at least 4. 15 | /// Write a method named isZeroPlentiful 16 | /// which returns the 17 | /// number of zero sequences if its array 18 | /// argument is zero-plentiful, otherwise it returns 0. 19 | /// 20 | /// 21 | /// 22 | public static int isZeroPlentiful(int[] a) 23 | { 24 | int isZero = 1; 25 | int count = 0; 26 | int zerocount = 0; 27 | for (int i = 0; i < a.Length && isZero==1 ; i++) 28 | { 29 | int current = a[i]; 30 | if (current == 0) 31 | zerocount++; 32 | else if (zerocount >= 4) { count++; zerocount = 0; isZero = 1; } 33 | else if (zerocount > 0) isZero = 0; 34 | 35 | 36 | if(i == a.Length - 1) 37 | { 38 | if (zerocount >= 4) { count++; zerocount = 0; } 39 | 40 | } 41 | } 42 | 43 | 44 | 45 | if (isZero == 1) isZero = count; 46 | return isZero; 47 | } 48 | 49 | 50 | 51 | /// 52 | /// A number is called digit-increasing if it is 53 | /// equal to n + nn + nnn + … for some digit n 54 | /// between 1 and 9. For example 24 is 55 | /// digit-increasing because it equals 2 + 22 (here n = 2) 56 | /// 57 | /// 58 | /// 59 | public static int isDigitIncreasing(int n) 60 | { 61 | int isDigitI = 0; 62 | 63 | int digitcount = 0; 64 | int tempn = n; 65 | do 66 | { 67 | tempn = tempn / 10; 68 | digitcount++; 69 | } while (tempn >=1); 70 | 71 | for (int i = 1; i <= 9 && isDigitI == 0; i++) 72 | { 73 | int sum = 0; 74 | int digit = 0; 75 | 76 | for (int j = 1; j <= digitcount; j++) 77 | { 78 | int ten = 1; 79 | if (j > 1) 80 | ten *= 10; 81 | sum = sum * ten; 82 | sum += i; 83 | 84 | digit += sum; 85 | } 86 | 87 | if (digit == n) isDigitI = 1; 88 | } 89 | 90 | return isDigitI; 91 | } 92 | 93 | /// 94 | /// An integer number can be encoded as an array as follows. 95 | /// Each digit n of the number is represented by n zeros followed by a 1. 96 | /// So the digit 5 is represented by 0, 0, 0, 0, 0, 1. 97 | /// The encodings of each digit of a number are combined 98 | /// to form the encoding of the number. 99 | /// So the number 1234 is encoded as the 100 | /// array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. 101 | /// The first 0, 1 is contributed by the digit 1, 102 | /// the next 0, 0, 1 is contributed by the digit 2, and so on. 103 | /// There is one other encoding rule: if the number is negative, 104 | /// the first element of the encoded array must be -1, so -201 is 105 | /// encoded as {-1, 0, 0, 1, 1, 0, 1}. Note that the 0 digit is 106 | /// represented by no zeros, i.e. there are two consecutive ones! 107 | /// Write a method named decodeArray 108 | /// that takes an encoded array and decodes it to return the number. 109 | /// You may assume that the input array is a legal encoded array, 110 | /// i.e., that -1 will only appear as the first element, 111 | /// all elements are either 0, 1 or -1 and that the last element is 1. 112 | /// 113 | /// 114 | /// 115 | public static int decodeArray(int[] a) 116 | { 117 | int decodenumber = 0; 118 | int Positive = 1; 119 | int zerocount = 0; 120 | int ten = 1; 121 | for (int i = 0; i < a.Length; i++) 122 | { 123 | if (i == 0) 124 | { 125 | if (a[i] < 0) Positive = a[i]; 126 | } 127 | 128 | if (a[i] == 0) 129 | zerocount++; 130 | else 131 | { 132 | if (zerocount > 0) 133 | { 134 | decodenumber = decodenumber * ten; 135 | decodenumber += zerocount; 136 | zerocount = 0; 137 | ten = 10; 138 | } 139 | else 140 | { 141 | decodenumber = decodenumber * ten; 142 | decodenumber += 0; 143 | ten = 10; 144 | } 145 | } 146 | 147 | } 148 | 149 | decodenumber = decodenumber * Positive; 150 | return decodenumber; 151 | } 152 | 153 | 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /MUMSPT/TestSet21/TestSet21.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet21 8 | { 9 | public static class TestSet21 10 | { 11 | /// 12 | /// An array is called systematically increasing if it 13 | /// consists of increasing sequences of the numbers from 1 to n. 14 | /// The first six (there are over 65,000 of them) systematically increasing arrays are: 15 | /// {1} 16 | /// {1, 1, 2} 17 | /// {1, 1, 2, 1, 2, 3} 18 | /// {1, 1, 2, 1, 2, 3, 1, 2, 3, 4} 19 | /// {1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5} 20 | /// {1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6} 21 | /// Write a function named isSystematicallyIncreasing which 22 | /// returns 1 if its array argument is systematically increasing. Otherwise it returns 0. 23 | /// 24 | /// 25 | /// 26 | public static int isSystematicallyIncreasing(int[] a) 27 | { 28 | 29 | int currentSequence = 0; 30 | 31 | // Recusive Check for sequence 32 | return CheckcheckSquence(a, 0, ++currentSequence); 33 | } 34 | 35 | static int CheckcheckSquence(int[] a, int startindex, int currentsequence) 36 | { 37 | int isSequence = 1; 38 | int i = 0; 39 | int current = 0; 40 | for (i = startindex; i < a.Length && currentsequence >= a[i] && isSequence == 1; i++) 41 | { 42 | current++; 43 | if (currentsequence >= a[i]) 44 | { 45 | if (current != a[i]) 46 | { 47 | isSequence = 0; 48 | } 49 | } 50 | else 51 | { 52 | if (current != a[i]) 53 | { 54 | isSequence = 0; 55 | } 56 | } 57 | } 58 | startindex = i; 59 | if (startindex < a.Length && isSequence == 1) 60 | return CheckcheckSquence(a, startindex, ++currentsequence); 61 | else return isSequence; 62 | } 63 | 64 | /// 65 | /// A positive, non-zero number n is a 66 | /// factorial prime if it is equal to factorial(n) + 1 67 | /// for some n and it is prime. Recall that factorial(n) 68 | /// is equal to 1 * 2 * … * n-1 * n. If you understand recursion, 69 | /// the recursive definition is 70 | /// factorial(1) = 1; 71 | /// factorial(n) = n*factorial(n-1). 72 | /// For example, factorial(5) = 1*2*3*4*5 = 120. 73 | /// Recall that a prime number is a natural number 74 | /// which has exactly two distinct natural number divisors: 1 and itself. 75 | /// Write a method named isFactorialPrime which 76 | /// returns 1 if its argument is a factorial prime number, otherwise it returns 0. 77 | /// 78 | /// 79 | /// 80 | public static int isFactorialPrime(int n) 81 | { 82 | int isFacorialPrime = 0; 83 | 84 | int currentfacorial = 1; 85 | for (int i = 1; i <= n && isFacorialPrime==0; i++) 86 | { 87 | currentfacorial = Factorial(i); 88 | if (n == currentfacorial + 1) 89 | { 90 | if (Helper.isPrime(n)) isFacorialPrime = 1; 91 | } 92 | } 93 | 94 | 95 | return isFacorialPrime; 96 | } 97 | 98 | private static int Factorial(int v) 99 | { 100 | if (v == 1) return 1; 101 | else return v * Factorial(v - 1); 102 | } 103 | 104 | 105 | 106 | /// 107 | /// Write a function named largestDifferenceOfEvens 108 | /// which returns the largest difference between even 109 | /// valued elements of its array argument. 110 | /// For example largestDifferenceOfEvens(new int[ ]{-2, 3, 4, 9}) 111 | /// returns 6 = (4 – (-2)). 112 | /// If there are fewer than 2 even numbers in the array, 113 | /// largestDifferenceOfEvens should return -1. 114 | /// 115 | /// 116 | /// 117 | public static int largestDifferenceOfEvens(int[] a) 118 | { 119 | int largestDiff = 0; 120 | 121 | int maxevent = 0, minevent = 0,eventcount = 0; 122 | 123 | for (int i = 0; i < a.Length; i++) 124 | { 125 | if(a[i] %2 == 0) { 126 | if(eventcount == 0) 127 | { 128 | maxevent = a[i]; 129 | minevent = a[i]; 130 | } 131 | eventcount++; 132 | if(a[i] > maxevent) 133 | { 134 | maxevent = a[i]; 135 | } 136 | if(a[i] < minevent) 137 | { 138 | minevent = a[i]; 139 | } 140 | 141 | largestDiff = maxevent - minevent; 142 | } 143 | } 144 | 145 | if(eventcount <2) 146 | { 147 | largestDiff = -1; 148 | } 149 | 150 | return largestDiff; 151 | 152 | 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /MUMSPT/TestSet4/TestSet4.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet4 8 | { 9 | public static class TestSet4 10 | { 11 | /// 12 | /// It is a fact that there exist two numbers x and y such that x! + y! = 10!. Write a method named solve10 that returns the values x and y in an array. 13 | /// The notation n! is called n factorial and is equal to n * n-1 * n-2 * … 2 * 1, e.g., 5! = 5*4*3*2*1 = 120. 14 | /// 15 | /// 16 | /// 17 | public static int[] solve10() 18 | { 19 | int[] factors = new int[2]; 20 | 21 | int tenFactorial = 1; 22 | int Factorialx = 1, Factorialy = 1; 23 | int x = 1, y = 1; 24 | bool solved = false; 25 | for (int i = 1; i <= 10; i++) 26 | { 27 | tenFactorial *= i; 28 | } 29 | 30 | for (x = 1; x <= 10 && solved == false; x++) 31 | { 32 | Factorialx *= x; 33 | Factorialy = 1; 34 | for (y = 1; y <= 10 && solved == false; y++) 35 | { 36 | Factorialy *= y; 37 | solved = (Factorialx + Factorialy == tenFactorial); 38 | } 39 | } 40 | if(!solved) 41 | { 42 | x = y = 0; 43 | } 44 | factors[0] = x; 45 | factors[1] = y; 46 | 47 | return factors; 48 | } 49 | 50 | public static int[] solve101() 51 | { 52 | int[] solve10 = new int[2]; 53 | int tenFactorial = 1; 54 | int x = 0; 55 | int y = 0; 56 | Boolean factorialFound = false; 57 | for (int i = 1; i <= 10; i++) 58 | { 59 | tenFactorial = tenFactorial * i; 60 | } 61 | for (x = 0; x < 10; x++) 62 | { 63 | int xFactorial = 1; 64 | if (x > 0) 65 | { 66 | for (int i = 1; i <= x; i++) 67 | { 68 | xFactorial = xFactorial * i; 69 | } 70 | } 71 | for (y = 0; y < 10; y++) 72 | { 73 | int yFactorial = 1; 74 | if (y > 0) 75 | { 76 | for (int j = 1; j <= y; j++) 77 | { 78 | yFactorial = yFactorial * j; 79 | } 80 | } 81 | if (xFactorial + yFactorial == tenFactorial) 82 | { 83 | factorialFound = true; 84 | break; 85 | } 86 | } 87 | if (factorialFound) 88 | { 89 | break; 90 | } 91 | } 92 | if (x == 10 && y == 10) 93 | { 94 | //Not Found 95 | x = 0; 96 | y = 0; 97 | } 98 | solve10[0] = x; 99 | solve10[1] = y; 100 | 101 | return solve10; 102 | } 103 | 104 | /// 105 | /// An array can hold the digits of a number. 106 | /// For example the digits of the number 32053 are stored in the array {3, 2, 0, 5, 3}. 107 | /// Write a method call repsEqual that takes an array and an integer and returns 1 108 | /// if the array contains only the digits of the number in the same order 109 | /// that they appear in the number. Otherwise it returns 0. 110 | /// 111 | /// 112 | /// 113 | /// 114 | public static int repsEqual(int[] a, int n) 115 | { 116 | int repsEqual = 1; 117 | 118 | for (int i = a.Length -1 ; i >= 0 && repsEqual==1; i--) 119 | { 120 | 121 | int lastDigit = n % 10; 122 | if (a[i] != lastDigit) 123 | repsEqual = 0; 124 | n = n / 10; 125 | } 126 | 127 | return repsEqual; 128 | } 129 | 130 | 131 | /// 132 | /// An array is called centered-15 if some consecutive sequence 133 | /// of elements of the array sum to 15 and this sequence is preceded 134 | /// and followed by the same number of elements. For example 135 | /// {3, 2, 10, 4, 1, 6, 9} is centered-15 because 136 | /// the sequence 10, 4, 1 sums to 15 and the sequence is preceded 137 | /// by two elements (3, 2) and followed by two elements(6,9). 138 | /// 139 | /// 140 | /// 141 | /// 142 | public static int isCentered15(int[] a) 143 | { 144 | int isCentered15 = 0; 145 | 146 | for (int i = 0; i < a.Length && isCentered15 == 0; i++) 147 | { 148 | int sum = 0; 149 | for (int j = i; j < a.Length && isCentered15 == 0 && sum < 15; j++) 150 | { 151 | sum += a[j]; 152 | if (sum == 15 && ((i - 0) == ((a.Length - 1) - j))) 153 | { 154 | isCentered15 = 1; 155 | } 156 | } 157 | } 158 | 159 | return isCentered15; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /MUMSPT/MUMSPT.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {57EC70D8-C4BE-4CED-BC70-54347C4ACE4B} 8 | Exe 9 | Properties 10 | MUMSPT 11 | MUMSPT 12 | v4.6 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 124 | -------------------------------------------------------------------------------- /MUMSPT/TestSet23/TestSet23.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MUMSPT.TestSet23 8 | { 9 | public static class TestSet23 10 | { 11 | /// 12 | /// A number n is vesuvian if it is the sum 13 | /// of two different pairs of squares. 14 | /// For example, 50 is vesuvian because 50 == 25 + 25 15 | /// and 1 + 49. The numbers 65 (1+64, 16+49) and 85 16 | /// (4+81, 36+49) are also vesuvian. 789 of 17 | /// the first 10,000 integers are vesuvian. 18 | /// 19 | /// 20 | /// 21 | public static int vesuvian(int n) 22 | { 23 | int isVesuvian = 0; 24 | 25 | int paircount = 0; 26 | for (int i = 0; i < n && isVesuvian == 0; i++) 27 | { 28 | if (Helper.isPerfectSquare(i))// is square 29 | { 30 | for (int j = 0; j < n && isVesuvian == 0; j++) 31 | { 32 | if (Helper.isPerfectSquare(j)) 33 | { 34 | if (i + j == n) 35 | paircount++; 36 | if (paircount == 2) 37 | isVesuvian = 1; 38 | } 39 | } 40 | } 41 | } 42 | 43 | return isVesuvian; 44 | } 45 | 46 | 47 | /// 48 | /// Define an array to be one-balanced if 49 | /// begins with zero or more 1s followed 50 | /// by zero or more non-1s and concludes 51 | /// with zero or more 1s. Write a function 52 | /// named isOneBalanced that returns 1 if 53 | /// its array argument is one-balanced, 54 | /// otherwise it returns 0. 55 | /// 56 | /// 57 | /// 58 | public static int isOneBalanced(int[] a) 59 | { 60 | 61 | int isOneBalanced = 1; 62 | 63 | int nonOneSequence = 0, oneSequence = 0; 64 | int firstOneSquenceCount = 0, secondOneSquenceCount = 0, nonOneSquenceCount = 0; 65 | 66 | for (int i = 0; i < a.Length && isOneBalanced == 1; i++) 67 | { 68 | int current = a[i]; 69 | if (current == 1) 70 | { 71 | if (oneSequence == 0 && nonOneSequence == 0) 72 | { 73 | oneSequence++; 74 | if (firstOneSquenceCount == 0) 75 | { 76 | firstOneSquenceCount++; 77 | } 78 | } 79 | else 80 | { 81 | if (nonOneSequence == 0) 82 | { 83 | if (oneSequence == 1) 84 | firstOneSquenceCount++; 85 | if (oneSequence == 2) 86 | secondOneSquenceCount++; 87 | } 88 | else 89 | { 90 | if (nonOneSequence == 1 && oneSequence == 1) 91 | oneSequence++; 92 | 93 | if (oneSequence == 2 || nonOneSequence == 1) 94 | { 95 | secondOneSquenceCount++; 96 | } 97 | 98 | } 99 | } 100 | } 101 | else 102 | { 103 | if (nonOneSequence == 0) 104 | { 105 | nonOneSequence++; 106 | if (nonOneSquenceCount == 0) 107 | { 108 | nonOneSquenceCount++; 109 | } 110 | 111 | } 112 | else 113 | { 114 | if (oneSequence == 1 || oneSequence == 0) 115 | { 116 | nonOneSquenceCount++; 117 | } 118 | if (oneSequence == 2) 119 | { 120 | isOneBalanced = 0; 121 | } 122 | } 123 | } 124 | } 125 | 126 | if (firstOneSquenceCount + secondOneSquenceCount != nonOneSquenceCount) 127 | isOneBalanced = 0; 128 | return isOneBalanced; 129 | } 130 | 131 | 132 | /// 133 | /// The Fibonacci sequence of numbers 134 | /// is 1, 1, 2, 3, 5, 8, 13, 21, 34, 135 | /// … The first and second numbers 136 | /// are 1 and after that ni = ni-2 + ni-1, 137 | /// e.g., 34 = 13 + 21. 138 | /// Write a method with signature 139 | /// int isFibonacci(int n) which returns 1 140 | /// if its argument is a number in the Fibonacci sequence, 141 | /// otherwise it returns 0. For example, isFibonacci(13) 142 | /// returns a 1 and isFibonacci(27) returns a 0. 143 | /// Your solution must not use 144 | /// recursion because unless you cache the Fibonacci 145 | /// numbers as you find them, 146 | /// the recursive solution recomputes 147 | /// the same Fibonacci number many times. 148 | /// 149 | /// 150 | /// 151 | public static int isFibonacci(int n) 152 | { 153 | int isFibonacci = 0; 154 | 155 | int a = 0, b = 1, c = a + b; 156 | 157 | do 158 | { 159 | a = b; 160 | b = c; 161 | c = a + b; 162 | if (c == n) 163 | isFibonacci = 1; 164 | } while (c <=n && isFibonacci==0); 165 | 166 | return isFibonacci; 167 | } 168 | } 169 | } 170 | --------------------------------------------------------------------------------