Source Code

for file S02_AJAXCoreSamples/CalcFactors.svc

<%@ ServiceHost Language="C#" Debug="true" Service="CalcFactors" %>

using System;
using System.ServiceModel;

/// <summary>The CalcFactors class that implements the service.
/// There is no need to do that inline, but it keeps "things" together if you do so.</summary>
[ServiceContract]
public class CalcFactors {

  /// <summary>Calculate all prime factors from a given number.</summary>
  /// <param name="inputText">A positive number</param>
  /// <returns>the list of all prime factors</returns>
  [OperationContract]
  public string CalcPrimeFactors(ulong number) {
    string outputText = String.Empty;
    ulong prime;  // try this factor (only primes will match!)

    prime = 2; // start with 2

    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;
    } // if
    return (outputText.Trim());
  } // CalcPrimeFactors

} // CalcFactors 

// End.

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

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