Beispiel #1
0
        private static async Task PrimeNoDecomp(CalculatorService.CalculatorServiceClient calculatorClient)
        {
            var primeNoDecompRequest = new PrimeNoDecompRequest()
            {
                Number = 120
            };
            var PrimeNoDecompResponse = calculatorClient.PrimeNoDecomp(primeNoDecompRequest);

            while (await PrimeNoDecompResponse.ResponseStream.MoveNext())
            {
                Console.WriteLine(PrimeNoDecompResponse.ResponseStream.Current.Factor);
                await Task.Delay(200);
            }
        }
        public override async Task PrimeNoDecomp(PrimeNoDecompRequest request, IServerStreamWriter <PrimeNoDecompResponse> responseStream, ServerCallContext context)
        {
            int k = 2;
            int N = request.Number;

            while (N > 1)
            {
                if (N % k == 0) // if k evenly divides into N
                {
                    await responseStream.WriteAsync(new PrimeNoDecompResponse()
                    {
                        Factor = k
                    });     // this is a factor

                    N /= k; // divide N by k so that we have the rest of the number left.
                }
                else
                {
                    k += 1;
                }
            }
        }