<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: numbers code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 15:10:27 GMT</pubDate>
    <description>DZone Snippets: numbers code</description>
    <item>
      <title>A solution for the "Carmichael Numbers" problem</title>
      <link>http://snippets.dzone.com/posts/show/5429</link>
      <description>A solution for the "Carmichael Numbers" problem.&lt;br /&gt;&lt;br /&gt;Problem description:&lt;br /&gt;&lt;a href="http://icpcres.ecs.baylor.edu/onlinejudge/external/100/10006.html"&gt;http://icpcres.ecs.baylor.edu/onlinejudge/external/100/10006.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Author: &lt;a href="http://www.inf.ufrgs.br/~jmftrindade"&gt;Joana Matos Fonseca da Trindade&lt;/a&gt;&lt;br /&gt;Date: 2008.04.24&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* &lt;br /&gt; * Solution for "Carmichael Numbers" problem.&lt;br /&gt; * UVa ID: 10006&lt;br /&gt; */&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;math.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAXPRIME 65001&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * Modular exponentiation algorithm. Returns b^e mod m.&lt;br /&gt; */&lt;br /&gt;unsigned long long fast_mod_pow(unsigned long long b, unsigned long long e, unsigned long long m) {&lt;br /&gt;    unsigned long long r = 1;&lt;br /&gt;    while (e &gt; 0) {&lt;br /&gt;        if ((e &amp; 1) == 1) {&lt;br /&gt;	    r = (r * b) % m;&lt;br /&gt;        }&lt;br /&gt;        e &gt;&gt;= 1;&lt;br /&gt;        b = (b * b) % m;&lt;br /&gt;    }&lt;br /&gt;    return r;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* &lt;br /&gt; * Generates all prime numbers up to MAXPRIME. Based on&lt;br /&gt; * the Sieve of Eratosthenes.&lt;br /&gt; */&lt;br /&gt;void gen_primes(bool p[]) {&lt;br /&gt;    p[0] = p[1] = false;&lt;br /&gt;	&lt;br /&gt;    /* &lt;br /&gt;     * starting at number 2 and going to the upper limit, mark &lt;br /&gt;     * all numbers as potential primes &lt;br /&gt;     */  &lt;br /&gt;    for (int i=2; i&lt;MAXPRIME; i++) {&lt;br /&gt;        p[i] = true;&lt;br /&gt;    }&lt;br /&gt;	&lt;br /&gt;    int m = floor(sqrt(MAXPRIME));&lt;br /&gt;    int n;&lt;br /&gt;    /* &lt;br /&gt;     * mark all multiples of a prime as non-primes. this has to be done for primes &lt;br /&gt;     * only up to the square root, since every number in the array has at least &lt;br /&gt;     * one factor smaller than the square root of the limit. &lt;br /&gt;     */&lt;br /&gt;    for (int i=2; i&lt;m; i++) {&lt;br /&gt;        if (p[i]) {&lt;br /&gt;       	    n = MAXPRIME / i;&lt;br /&gt;	    for (int j=2; j&lt;=n; j++) {&lt;br /&gt;	        p[i * j] = false;&lt;br /&gt;	    }&lt;br /&gt;	}&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* generates all carmichael numbers up to the given limit by performing the fermat test */&lt;br /&gt;void gen_carmi(bool c[], bool p[]) {&lt;br /&gt;&lt;br /&gt;    /* initialize carmichael numbers array with false */&lt;br /&gt;    memset(c, 0, MAXPRIME * sizeof(bool));&lt;br /&gt;	&lt;br /&gt;    /* &lt;br /&gt;     * starting from the first non-prime, mark all &lt;br /&gt;     * odd numbers as potential carmichael numbers &lt;br /&gt;     */&lt;br /&gt;    for (int i=9; i&lt;MAXPRIME; i+=2) {&lt;br /&gt;	c[i] = true;&lt;br /&gt;    }&lt;br /&gt;	&lt;br /&gt;    /* &lt;br /&gt;     * again, for all odd numbers, we exclude the primes and perform&lt;br /&gt;     * the fermat test for 2 &lt;= a &lt;= n-1.&lt;br /&gt;     */&lt;br /&gt;    for (int n=9; n&lt;MAXPRIME; n+=2) {&lt;br /&gt;	/* VERY IMPORTANT! check first if this number is prime, otherwise we get TLE */&lt;br /&gt;	if (p[n]) {&lt;br /&gt;	    c[n] = false;&lt;br /&gt;	    continue;&lt;br /&gt;	}&lt;br /&gt;	for (int a=2; a&lt;=n-1; a++) {&lt;br /&gt;	    if (fast_mod_pow(a,n,n) != a) {&lt;br /&gt;	        c[n] = false;&lt;br /&gt;		break;&lt;br /&gt;	    } &lt;br /&gt;	}	&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* main */&lt;br /&gt;int main() {&lt;br /&gt;    unsigned long long n; /* number */&lt;br /&gt;    unsigned long long a; /* a of the fermat test */&lt;br /&gt;    bool prime[MAXPRIME]; /* prime numbers array */&lt;br /&gt;    bool carmi[MAXPRIME]; /* carmichael numbers array */&lt;br /&gt;	&lt;br /&gt;    gen_primes(prime);&lt;br /&gt;    gen_carmi(carmi, prime);&lt;br /&gt;	&lt;br /&gt;    while (cin &gt;&gt; n &amp;&amp; (n != 0)) {	&lt;br /&gt;        if (carmi[n]) {&lt;br /&gt;            cout &lt;&lt; "The number " &lt;&lt; n &lt;&lt; " is a Carmichael number." &lt;&lt; endl;&lt;br /&gt;        } else {&lt;br /&gt;            cout &lt;&lt; n &lt;&lt; " is normal." &lt;&lt; endl;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 Apr 2008 23:16:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5429</guid>
      <author>jmftrindade (Joana M. F. da Trindade)</author>
    </item>
    <item>
      <title>ruby is_numeric? extension to String</title>
      <link>http://snippets.dzone.com/posts/show/5003</link>
      <description>&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def is_numeric?&lt;br /&gt;    Float self rescue false&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 18 Jan 2008 07:46:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5003</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Euclid's extended algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4192</link>
      <description>Basically Euclid's algorithm for finding the largest common denominator, this one is modified in order to obtain the numbers d, x, y, where d is the dennominator and they check the following equation:&lt;br /&gt;d = ax + by&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def euclidExtended(a, b):&lt;br /&gt;  if b == 0:&lt;br /&gt;    return a, 1, 0&lt;br /&gt;  dd, xx, yy = euclidExtended(b, a % b)&lt;br /&gt;  d, x, y = dd, yy, xx - int(a / b) * yy&lt;br /&gt;  return d, x, y&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Jun 2007 14:59:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4192</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Perfect Numbers</title>
      <link>http://snippets.dzone.com/posts/show/3033</link>
      <description>// School project&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/*&lt;br /&gt;	Bryan Abrams&lt;br /&gt;	Prof Bioano&lt;br /&gt;	Struct Programming TR at 11&lt;br /&gt;	Homework Five (perfect numbers)&lt;br /&gt;	For Description, see Explain();&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;//-----start prototypes-----//&lt;br /&gt;void Explain();&lt;br /&gt;int GatherUserData(int,int);&lt;br /&gt;int AnalyzeNumber(int);&lt;br /&gt;void PrintResults(int,int,int);&lt;br /&gt;//-----end   prototypes-----//&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;	int num1, numRet, numRet2;&lt;br /&gt;	char cont = 'Y';&lt;br /&gt;&lt;br /&gt;	Explain();&lt;br /&gt;&lt;br /&gt;	do {&lt;br /&gt;		cout &lt;&lt; "\nPlease, enter an integer: ";&lt;br /&gt;		cin &gt;&gt; num1;&lt;br /&gt;&lt;br /&gt;		numRet = AnalyzeNumber(num1);&lt;br /&gt;		numRet2 = GatherUserData(num1,numRet);&lt;br /&gt;		PrintResults(num1,numRet,numRet2);&lt;br /&gt;&lt;br /&gt;		cout &lt;&lt; "Do you wish to continue? ";&lt;br /&gt;		cin &gt;&gt; cont;&lt;br /&gt;	}while(toupper(cont) == 'Y');&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void Explain()&lt;br /&gt;{&lt;br /&gt;	cout &lt;&lt; "A number is said to be perfect if the sum of its divisors (except for itself)\n"&lt;br /&gt;		 &lt;&lt; "is equal to itself. For example 6, is a perfect number because the sum of its\n"&lt;br /&gt;		 &lt;&lt; "divisors (1 + 2 + 3) is equal to 6. The number 8 is said to be defiecient\n"&lt;br /&gt;		 &lt;&lt; "because the sum of its divisors (1 + 2 + 4) is only 7. The Number 12 is said\n"&lt;br /&gt;		 &lt;&lt; "to be abudant because the sum of its divisors (1 + 2 + 3 + 4 + 5 + 6) is 16.\n\n";&lt;br /&gt;	return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int GatherUserData(int num1, int num2)&lt;br /&gt;{&lt;br /&gt;	if(num2 &gt; num1)&lt;br /&gt;		return 3; // abudant&lt;br /&gt;	else if(num2 &lt; num1)&lt;br /&gt;		return 2; // deficient&lt;br /&gt;	else&lt;br /&gt;		return 1; // perfect&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int AnalyzeNumber(int num)&lt;br /&gt;{&lt;br /&gt;	int numberTot = 0;&lt;br /&gt;	int numberReturn = 0;&lt;br /&gt;&lt;br /&gt;	for(int i = 1; i &lt;= num; i++)&lt;br /&gt;	{&lt;br /&gt;		numberTot = num % i;&lt;br /&gt;		if((numberTot == 0) &amp;&amp; (i != num))&lt;br /&gt;		{&lt;br /&gt;			numberReturn += i;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return numberReturn;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void PrintResults(int num1, int retNum, int retNum2)&lt;br /&gt;{&lt;br /&gt;	cout &lt;&lt; "The sum of the factors of " &lt;&lt; num1 &lt;&lt; " is " &lt;&lt; retNum &lt;&lt; endl;&lt;br /&gt;	if(retNum2 == 1)&lt;br /&gt;		cout &lt;&lt; num1 &lt;&lt; " is a perfect number." &lt;&lt; endl;&lt;br /&gt;	else if(retNum2 == 2)&lt;br /&gt;		cout &lt;&lt; num1 &lt;&lt; " is a deficient number." &lt;&lt; endl;&lt;br /&gt;	else if(retNum2 == 3)&lt;br /&gt;		cout &lt;&lt; num1 &lt;&lt; " is a abundant number." &lt;&lt; endl;&lt;br /&gt;&lt;br /&gt;	return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 24 Nov 2006 22:24:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3033</guid>
      <author>dpbBryan (Bryan Abrams)</author>
    </item>
    <item>
      <title>Ordinals for All Numeric Types</title>
      <link>http://snippets.dzone.com/posts/show/2368</link>
      <description>This snippet will extend all numerical types with a method for returning the English ordinal, i.e. 1st, 2nd, 3rd, 4th, etc.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Numeric&lt;br /&gt;  def ordinal&lt;br /&gt;    cardinal = self.to_i.abs&lt;br /&gt;    if (10...20).include?(cardinal) then&lt;br /&gt;      cardinal.to_s &lt;&lt; 'th'&lt;br /&gt;    else&lt;br /&gt;      cardinal.to_s &lt;&lt; %w{th st nd rd th th th th th th}[cardinal % 10]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;[1, 22, 123, 10, -3.1415].collect { |i| i.ordinal }&lt;br /&gt;=&gt; ["1st", "22nd", "123rd", "10th", "3rd"]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.</description>
      <pubDate>Fri, 04 Aug 2006 22:46:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2368</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Float to HTML fraction entity</title>
      <link>http://snippets.dzone.com/posts/show/2323</link>
      <description>Uses HTML entities to display pretty fractional values for applicable floating point numbers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Float&lt;br /&gt;    def html_fraction&lt;br /&gt;        self.to_s.&lt;br /&gt;            sub(/(.+)\.0$/,     '\1'        ).  # Zero decimal&lt;br /&gt;            sub(/(-?\d+)\.25$/, '\1&amp;frac14;').  # One quarter&lt;br /&gt;            sub(/(-?\d+)\.5$/,  '\1&amp;frac12;').  # One half&lt;br /&gt;            sub(/(-?\d+)\.75$/, '\1&amp;frac34;').  # Three quarters&lt;br /&gt;            sub(/^(-?)0(&amp;.+)/,  '\1\2'      )   # Strip leading zeroes&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And it works something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; (0.1).html_fraction&lt;br /&gt;=&gt; 0.1&lt;br /&gt;&gt;&gt; (0.25).html_fraction&lt;br /&gt;=&gt; &amp;frac14;&lt;br /&gt;&gt;&gt; (0.50).html_fraction&lt;br /&gt;=&gt; &amp;frac12;&lt;br /&gt;&gt;&gt; (0.75).html_fraction&lt;br /&gt;=&gt; &amp;frac34;&lt;br /&gt;&gt;&gt; (1.0).html_fraction&lt;br /&gt;=&gt; 1.0&lt;br /&gt;&gt;&gt; (2.25).html_fraction&lt;br /&gt;=&gt; 2&amp;frac14;&lt;br /&gt;&gt;&gt; (3.5).html_fraction&lt;br /&gt;=&gt; 3&amp;frac12;&lt;br /&gt;&gt;&gt; (4.75).html_fraction&lt;br /&gt;=&gt; 4&amp;frac34;&lt;br /&gt;&gt;&gt; (5.85).html_fraction&lt;br /&gt;=&gt; 5.85&lt;br /&gt;&gt;&gt; (-1.5).html_fraction&lt;br /&gt;=&gt; -1&amp;frac12;&lt;br /&gt;&gt;&gt; (-1.6).html_fraction&lt;br /&gt;=&gt; -1.6;&lt;br /&gt;&gt;&gt; (-0.25).html_fraction&lt;br /&gt;=&gt; -&amp;frac14;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 24 Jul 2006 21:14:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2323</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>IsReallyInteger</title>
      <link>http://snippets.dzone.com/posts/show/1185</link>
      <description>&lt;code&gt;&lt;br /&gt;----------------------------------------------------------------------------&lt;br /&gt;--Purpose: Checks that the input string is really an integer of the specified type.  &lt;br /&gt;-- To be used in place of the ISNUMERIC function, as it can't be trusted.&lt;br /&gt;-- See http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=10194&lt;br /&gt;--Inspired by: http://www.aspfaq.com/show.asp?id=2390 &lt;br /&gt;--Created: 10/20/05 by Oskar Austegard.&lt;br /&gt;--Updated: 11/16/05 by Oskar Austegard - fixed bugs&lt;br /&gt;----------------------------------------------------------------------------&lt;br /&gt;ALTER FUNCTION dbo.IsReallyInteger&lt;br /&gt;(  &lt;br /&gt;  @Num varchar(64), --Input string to be checked&lt;br /&gt;  @Type varchar(8) --Type of integer: bigint, int, smallint, tinyint&lt;br /&gt;)  &lt;br /&gt;RETURNS BIT  &lt;br /&gt;BEGIN  &lt;br /&gt;  --Get the absolute value of the number by removing a leading - or +&lt;br /&gt;  DECLARE @AbsNum varchar(64), @Length tinyint&lt;br /&gt;  SET @AbsNum = CASE WHEN LEFT(@Num, 1) IN ('-', '+') THEN SUBSTRING(@Num, 2, LEN(@Num)) ELSE @Num END&lt;br /&gt;&lt;br /&gt;  --Remove leading zeros&lt;br /&gt;  WHILE LEN(@AbsNum) &gt; 1 AND LEFT(@AbsNum, 1) = '0'&lt;br /&gt;    SET @AbsNum = SUBSTRING(@AbsNum, 2, LEN(@AbsNum))&lt;br /&gt;&lt;br /&gt;  SET @Length = LEN(@AbsNum)  &lt;br /&gt;  --Reinsert the - in negative numbers&lt;br /&gt;  SET @Num = CASE WHEN LEFT(@Num, 1) = '-' THEN '-' + @AbsNum ELSE @AbsNum END&lt;br /&gt;&lt;br /&gt;  --Check for empty string or non-digits&lt;br /&gt;  IF @AbsNum = '' OR PATINDEX('%[^0-9]%', @AbsNum) &gt; 0&lt;br /&gt;    RETURN 0&lt;br /&gt;&lt;br /&gt;  --Check limits by type&lt;br /&gt;  IF (@Type = 'bigint' AND (@Length &lt; 19 OR (@Length = 19 AND (@AbsNum &lt; '9223372036854775807' OR @Num = '-9223372036854775808'))))&lt;br /&gt;    OR (@Type = 'int' AND (@Length &lt; 10 OR (@Length = 10 AND (@AbsNum &lt; '2147483648' OR @Num = '-2147483648'))))&lt;br /&gt;    OR (@Type = 'smallint' AND (@Length &lt; 5 OR (@Length = 5 AND (@AbsNum &lt; '32768' OR @Num = '-32768'))))&lt;br /&gt;    OR (@Type = 'tinyint' AND LEFT(@Num, 1) &lt;&gt; '-' AND (@Length &lt; 3 OR (@Length = 3 AND @AbsNum &lt; '256')))&lt;br /&gt;    RETURN 1 --Success&lt;br /&gt;  --Else&lt;br /&gt;  RETURN 0 --Failure&lt;br /&gt;END  &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:50:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1185</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Create a Number Table</title>
      <link>http://snippets.dzone.com/posts/show/1184</link>
      <description>Created 08/26/05 by Oskar Austegard (&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;) from article at &lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp"&gt;http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp&lt;/a&gt;&lt;br /&gt;Can be used inline in functions, or to create a standalone Numbers table (as required by &lt;a href="http://www.bigbold.com/snippets/posts/show/1183"&gt;dbo.Split&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--Creates a table of sequential numbers, useful for all sorts of things&lt;br /&gt;--Created 08/26/05 by Oskar Austegard from article at &lt;br /&gt;--http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp&lt;br /&gt;--Limits: @Min and @Max must be between -2147483647 and 2147483647, including.&lt;br /&gt;--If @Max &lt;= @Min, only a single record with @Min is created&lt;br /&gt;ALTER FUNCTION dbo.NumberTable (@Min int, @Max int)&lt;br /&gt;RETURNS @T TABLE (Number int NOT NULL PRIMARY KEY)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;  -- Seed the table with the min value&lt;br /&gt;  INSERT @T VALUES (@Min)&lt;br /&gt;  --Loop until all the rows are created, inserting ever more records for each iteration (1, 2, 4, etc)&lt;br /&gt;  WHILE @@ROWCOUNT &gt; 0&lt;br /&gt;	BEGIN&lt;br /&gt;	  INSERT @T &lt;br /&gt;	  --Get the next values by adding the current max - start value + 1 to each existing number&lt;br /&gt;	  --need to calculate increment value first to avoid arithmetic overflow near limits of int&lt;br /&gt;	  SELECT t.Number + (x.MaxNumber - @Min + 1)&lt;br /&gt;	  FROM @T t&lt;br /&gt;	    CROSS JOIN (SELECT MaxNumber = MAX(Number) FROM @T) x --Current max&lt;br /&gt;	  WHERE&lt;br /&gt;	    --Do not exceed the Max - shift the increment to the right side to take advantage of index&lt;br /&gt;	    t.Number &lt;= @Max - (x.MaxNumber - @Min + 1)&lt;br /&gt;	END&lt;br /&gt;  RETURN&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:47:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1184</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>number-lines function</title>
      <link>http://snippets.dzone.com/posts/show/1113</link>
      <description>&lt;code&gt;&lt;br /&gt;    number-lines: func [&lt;br /&gt;        "Insert leading line numbers for each line."&lt;br /&gt;        input [string! block!]&lt;br /&gt;        /local lines lead-wd&lt;br /&gt;    ][&lt;br /&gt;        lines: any [all [block? input  input] parse/all input form newline]&lt;br /&gt;        lead-wd: length? form length? lines&lt;br /&gt;        repeat i length? lines [&lt;br /&gt;            insert lines/:i join pad-num i lead-wd ": "&lt;br /&gt;        ]&lt;br /&gt;        either block? input [lines] [rejoin delimit lines newline]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 06:01:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1113</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Defining a custom validates in rails</title>
      <link>http://snippets.dzone.com/posts/show/822</link>
      <description>in the model:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  require 'validations'&lt;br /&gt;&lt;br /&gt;  validates_positive_or_zero :number&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;in /lib/validations.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validates_positive_or_zero(*attr_names)&lt;br /&gt;  configuration = { :message =&gt; "Cannot be negative" }&lt;br /&gt;  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;  validates_each attr_names do |m, a, v| m.errors.add(a, configuration[:message]) if v&lt;0 end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Oct 2005 21:47:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/822</guid>
      <author>zzzrByte (Tal Ater)</author>
    </item>
  </channel>
</rss>
