Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Hope to add this method Pow(BigInteger a, BigInteger b, BigInteger c) #110310

Closed
toolgood opened this issue Dec 2, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics

Comments

@toolgood
Copy link

toolgood commented Dec 2, 2024

Background and motivation

python code like

print(pow(2,3,5)) #   2*2*2%5 = 8 % 5 = 3
print(2*2*2%5)  #   pow(2,3,5) = 3

API Proposal

namespace System.Collections.Generic;

        public static BigInteger Pow(BigInteger a, BigInteger b, BigInteger c)
        {
            Dictionary<BigInteger, BigInteger> dict = new Dictionary<BigInteger, BigInteger>();
            BigInteger temp = a;
            BigInteger count = 1;
            dict[count] = temp;
            while (count * 2 <= b) {
                count = count * 2;
                temp = BigInteger.Pow(temp, 2) % c;
                dict[count] = temp;
            }

            var keys = dict.Keys.OrderByDescending(k => k).ToList();
            BigInteger aa = 1;
            for (int i = 0; i < keys.Count; i++) {
                if (b >= keys[i]) {
                    b = b - keys[i];
                    aa = (aa * dict[keys[i]]) % c;
                }
            }
            return aa;
        }

API Usage

var bbbb = Pow(2, 3, 5);

Alternative Designs

No response

Risks

No response

@toolgood toolgood added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 2, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 2, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

@vcsjones
Copy link
Member

vcsjones commented Dec 2, 2024

It seems like you are asking for BigInteger.ModPow. Does that API suit your needs?

@toolgood toolgood closed this as completed Dec 2, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Projects
None yet
Development

No branches or pull requests

2 participants