Skip to content

Commit 83aad28

Browse files
committed
Add project files.
1 parent 52f4fa5 commit 83aad28

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

FastModularExponentiation.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34714.143
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastModularExponentiationTest", "FastModularExponentiationTest\FastModularExponentiationTest.csproj", "{2ACD9A23-1C7C-4918-B877-6C749A46CA4B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastModularExponentiation", "FastModularExponentiation\FastModularExponentiation.csproj", "{E36D9AAA-5933-4198-BA6E-8B3BDACB41E6}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{2ACD9A23-1C7C-4918-B877-6C749A46CA4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{2ACD9A23-1C7C-4918-B877-6C749A46CA4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{2ACD9A23-1C7C-4918-B877-6C749A46CA4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{2ACD9A23-1C7C-4918-B877-6C749A46CA4B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E36D9AAA-5933-4198-BA6E-8B3BDACB41E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E36D9AAA-5933-4198-BA6E-8B3BDACB41E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E36D9AAA-5933-4198-BA6E-8B3BDACB41E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E36D9AAA-5933-4198-BA6E-8B3BDACB41E6}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {FD41D912-3746-4990-86DE-897E529DA4AC}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<Authors>Łukasz Górski</Authors>
9+
<Copyright>Copyright © 2024 Łukasz Górski</Copyright>
10+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace FastModularExponentiation
2+
{
3+
public class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
int y = ReadInteger("Enter a: ");
8+
int exp = ReadInteger("Enter exponent: ");
9+
int mod = ReadInteger("Enter modulo: ");
10+
11+
Console.WriteLine();
12+
13+
int result = ModularExponentiation(y, exp, mod);
14+
Console.WriteLine();
15+
Console.WriteLine($"Result: {result}");
16+
}
17+
18+
static int ReadInteger(string prompt)
19+
{
20+
Console.Write(prompt);
21+
return int.Parse(Console.ReadLine());
22+
}
23+
24+
public static int ModularExponentiation(int y, int exp, int mod)
25+
{
26+
string binary = Convert.ToString(exp, 2);
27+
char[] binaryArray = binary.ToCharArray();
28+
Array.Reverse(binaryArray);
29+
30+
int x = 1;
31+
32+
Console.WriteLine($"{"i",2} | {"x",8} | {"a",8} | {"t_i"}");
33+
34+
for (int i = 0; i < binaryArray.Length; ++i)
35+
{
36+
Console.WriteLine($"{i,2} | {x,8} | {y,8} | {binaryArray[i]}");
37+
x = binaryArray[i] == '0' ? x : (x * y) % mod;
38+
y = (y * y) % mod;
39+
}
40+
41+
Console.WriteLine($"{binaryArray.Length,2} | {x,8} | {y,8} | -");
42+
return x;
43+
}
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using FastModularExponentiation;
2+
3+
namespace FastModularExponentiationTest
4+
{
5+
public class FastModularExponentiationTest
6+
{
7+
[Theory]
8+
[InlineData(450, 289, 520, 320)] // 450^289 % 520 = 320
9+
[InlineData(2, 1234, 1117, 1038)] // 2^1234 % 1117 = 1038
10+
[InlineData(2, 638, 1019, 396)] // 2^638 % 1019 = 396
11+
[InlineData(2, 719, 1019, 486)] // 2^719 % 1019 = 486
12+
[InlineData(486, 638, 1019, 498)] // 486^638 % 1019 = 498
13+
[InlineData(396, 719, 1019, 498)] // 396^719 % 1019 = 498
14+
[InlineData(20, 1001, 1739, 1327)] // 20^1001 % 1739 = 1327
15+
[InlineData(1327, 761, 1739, 20)] // 1327^761 % 1739 = 20
16+
[InlineData(508, 1001, 1739, 1454)] // 508^1001 % 1739 = 1454
17+
[InlineData(357, 761, 1739, 1630)] // 357^761 % 1739 = 1630
18+
[InlineData(1630, 1001, 1739, 357)] // 1630^1001 % 1739 = 357
19+
[InlineData(2, 937, 1619, 1454)] // 2^937 % 1619 = 1454
20+
[InlineData(1454, 320, 1619, 1154)] // 1454^320 % 1619 = 1154
21+
[InlineData(2, 195, 1619, 1380)] // 2^195 % 1619 = 1380
22+
[InlineData(1454, 195, 1619, 1053)] // 1454^195 % 1619 = 1053
23+
[InlineData(130, 681, 1619, 571)] // 130^681 % 1619 = 571
24+
[InlineData(2, 515, 1619, 1310)] // 2^515 % 1619 = 1310
25+
[InlineData(2, 357, 1619, 983)] // 2^357 % 1619 = 983
26+
[InlineData(1454, 1310, 1619, 807)] // 1454^1310 % 1619 = 807
27+
[InlineData(1310, 1579, 1619, 902)] // 1310^1579 % 1619 = 902
28+
public void FastModularExponentiationTestCalculationTest(int y, int exp, int mod, int expected)
29+
{
30+
int result = Program.ModularExponentiation(y, exp, mod);
31+
Assert.Equal(expected, result);
32+
}
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
<Authors>Łukasz Górski</Authors>
11+
<Copyright>Copyright © 2024 Łukasz Górski</Copyright>
12+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="coverlet.collector" Version="6.0.2">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
21+
<PackageReference Include="xunit" Version="2.8.1" />
22+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
23+
<PrivateAssets>all</PrivateAssets>
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
</PackageReference>
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Reference Include="FastModularExponentiation">
30+
<HintPath>..\FastModularExponentiation\bin\Debug\net8.0\FastModularExponentiation.dll</HintPath>
31+
</Reference>
32+
</ItemGroup>
33+
34+
<ItemGroup>
35+
<Using Include="Xunit" />
36+
</ItemGroup>
37+
38+
</Project>

0 commit comments

Comments
 (0)