BVB Source Codes

Algorithm-Implementations Show HarshadNumbers.cs Source code

Return Download Algorithm-Implementations: download HarshadNumbers.cs Source code - Download Algorithm-Implementations Source code - Type:.cs
  1. 锘縰sing System;
  2. using System.Collections.Generic;
  3.  
  4. namespace HarshadNumbers
  5. {
  6.         public static class HarshadNumbers
  7.         {
  8.  
  9.                 /// <summary>
  10.                 /// Checks weather or not a number is a 10-Harshad Number
  11.                 /// </summary>
  12.                 /// <param name="l">The number to be tested</param>
  13.                 /// <returns>True if l is a Harshad Number</returns>
  14.                 public static bool IsHarshad(ulong l)
  15.                 {
  16.                         // We don't want division by zero, so let's make sure the universe doesn't explode
  17.                         if (l == 0)
  18.                                 return false;
  19.  
  20.                         ulong sum = 0;
  21.  
  22.                         // Loop through each digit, adding them to the total sum
  23.                         foreach (char c in l.ToString())
  24.                                 sum += ulong.Parse(c.ToString());
  25.  
  26.                         // If the remainder of the division is zero, the number is divisble.
  27.                         return (l % sum == 0);
  28.                 }
  29.  
  30.                 // Something extra, the method below is not essential
  31.  
  32.                 /// <summary>
  33.                 /// Generator method that generates all (computable) Harshad Numbers
  34.                 /// </summary>
  35.                 /// <returns>Harshad numbers, in crescent order, starting at 1</returns>
  36.                 public static IEnumerable<ulong> AllHarshadNumbers()
  37.                 {
  38.                         for (ulong l = 0; ; l++)
  39.                                 if (IsHarshad(l))
  40.                                         yield return l;
  41.                 }
  42.         }
  43. }
  44.  
downloadHarshadNumbers.cs Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top