(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* :Name: PwM`Chap06Procedural` *)


(* :Book: Programming with Mathematica: An Introduction *)


(* :Author: Paul Wellin *)


(* :Summary: This package contains the functions defined in Chapter 6, Procedural programming. *)


(* :Context: PwM`Chap06Procedural` *)


(* :Package Version: 1.0 *)


(* :Copyright: Copyright 2012, Cambridge University Press *)


(* :History: Version 1.0 by Paul Wellin, Dec 2012 *)


(* :Keywords: digit root, lucky number, upper triangular matrix *)


(* :Source: Chap06Procedural.nb *)


(* :Mathematica Version: 8.0.4 *)


Remove[DigitRoot,UpperTriangularMatrix]


BeginPackage["PwM`Chap06Procedural`"]


DigitRoot::usage="DigitRoot[\!\(\*
StyleBox[\"n\", \"TI\"]\)] returns the digital root of the integer \!\(\*
StyleBox[\"n\", \"TI\"]\).";


LuckyNumbers::usage="LuckyNumber[\!\(\*
StyleBox[\"n\", \"TI\"]\)] returns a list of the lucky numbers less than \!\(\*
StyleBox[\"n\", \"TI\"]\).";


UpperTriangularMatrix::usage="UpperTriangularMatrix[\!\(\*
StyleBox[\"n\", \"TI\"]\)] returns a square matrix of size \!\(\*
StyleBox[\"n\", \"TI\"]\) with zeros below the diagonal and ones on and above the diagonal.";


Begin["`Private`"]


DigitRoot[n_Integer?Positive]:=If[Mod[n,9]==0,9,Mod[n,9]]


LuckyNumbers[n_Integer?Positive]:=Module[{p,i=2,ints=Range[1,n,2]},
While[ints[[i]]<Length[ints],
p=ints[[i]];
ints=Drop[ints,p;;-1;;p];
i++];
ints]


UpperTriangularMatrix[{m_,n_},val_:1]:=Table[If[i>=j,0,val],{i,m},{j,n}]


End[]


EndPackage[]



