Source Code

for file S01_AsyncSamples/CalcService.asmx

<%@ WebService Language="C#" Class="CalcService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://www.mathertel.de/S01_AsyncSamples/",
  Description="A WebService for the calculation of prime factors.")]
// [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CalcService : System.Web.Services.WebService {
  
  /// <summary>Calculate all prime factors of a given number.</summary>
  /// <param name="inputText">a positive number</param>
  /// <returns>the list of all prime factors</returns>
  [WebMethod(Description="Calculate all prime factors of a given number.")]
  public string CalcPrimeFactors(string inputText) {
    string outputText = String.Empty;
    UInt64 prime;  // try this factor (only primes will match!)
    UInt64 number; // product of the remaining factors

    if ((inputText == null) || (inputText.Length == 0) || (inputText == "0"))
      return (null);

    prime = 2; // start with 2
    number = UInt64.Parse(inputText);

    while ((number > 1) && (prime * prime <= number)) {
      if (number % prime != 0) {
        // try the next factor (slowly)
        prime += (prime == 2UL ? 1UL : 2UL); 

      } else {
        // found a factor !
        outputText = outputText + " " + prime;
        number = number / prime;
      } // if
    } // while

    if (number > 1) {
      // the last factor (a prime) is here.
      outputText = outputText + " " + number;
    }

    return (outputText.Trim());

  } // CalcPrimeFactors
} // class


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.