// Get a positive number from the user. If the user inserts 'i_ExcludingStr', the function will return null instead public static uint?GetPositiveNumberFromUser(string i_MessageForUser, PositiveRange i_InputRange, string i_ExcludingStr) { string userInputStr; uint currUserNumericInput; bool isValidInput; uint? retUserInput = null; System.Console.WriteLine(i_MessageForUser); do { userInputStr = System.Console.ReadLine(); if ((i_ExcludingStr != null) && (i_ExcludingStr == userInputStr)) { break; } isValidInput = uint.TryParse(userInputStr, out currUserNumericInput) && i_InputRange.IsInRange(currUserNumericInput); if (!isValidInput) { System.Console.WriteLine("Invalid input! Please try again:"); } else { retUserInput = currUserNumericInput; } }while (!isValidInput); return(retUserInput); }
/// <summary> /// Initializes a new instance of the <see cref="PrimeSieve"/> class. /// </summary> /// <param name="upTo"> /// The upper bound of the sieveRange to be sieved, this means, /// the sieveRange [1,n] is searched for prime numbers. /// </param> public PrimeSieve(int upTo) { this.primes = new int[GetPiHighBound(upTo)]; this.sieveRange = new PositiveRange(1, upTo); this.numberOfPrimes = this.MakePrimeList(upTo); this.primeRange = new PositiveRange(1, this.numberOfPrimes); }
/// <summary> /// Initializes the prime collection for the given sieve. /// </summary> /// <param name="sieve">The sieve, the prime numbers of which /// are to be represented by a collection.</param> public PrimeCollection(PrimeSieve sieve) { this.sieve = sieve; this.end = sieve.numberOfPrimes - 1; this.enumRange = sieve.sieveRange; this.primeRange = sieve.primeRange; this.increment = 1; }
public WindowsUI(string i_Title, PositiveRange i_BoardRange) { r_BoardRange = i_BoardRange; //todo: is still required? // r_ParamsDialog = new ParamsDialogForm(i_BoardRange); //todo: ENFORCE MAX TEXT SIZE!!!!!!!!!!!! otherwise names very long.. also enforce computer name // r_ParamsDialog.Show(); // r_ReversedTicTacToeParams = new ReversedTicTacToeParams(r_ParamsDialog.GameType, r_ParamsDialog.BoardSize, r_ParamsDialog.PlayerNames); r_ReversedTicTacToeParams = new ReversedTicTacToeParams(new GameType(GameType.eGameType.PlayerVsPlayer), 5, new string[] { "Player 1", "PPP" }); r_BoardCellChoosingRange = new TwoDimensionalPositiveRange(k_BoardMinChoice, r_ReversedTicTacToeParams.BoardSize, k_BoardMinChoice, r_ReversedTicTacToeParams.BoardSize); r_GameWindow = new GameWindowForm(i_Title, r_ReversedTicTacToeParams.BoardSize); }
/// <summary> /// Computes the Product of the prime numbers in the given sieveRange. /// </summary> /// <param name="range">The sieveRange of the enumeration.</param> /// <returns>The Product of the prime numbers in the enumeration. /// </returns> public XInt GetPrimorial(PositiveRange range) { int start, size; var pc = new PrimeCollection(this, range); if (pc.GetSliceParameters(out start, out size)) { return(XInt.One); } return(XMath.Product(this.primes, start, size)); }
/// <summary> /// Computes the Product of the prime numbers in the given sieveRange /// primes[start]*primes[start+increment]*primes[start+2*increment]*... /// </summary> /// <param name="low">Lower bound of the sieveRange of the enumeration.</param> /// <param name="high">Higher bound of the sieveRange of the enumeration.</param> /// <returns>The Product of the prime numbers in the enumeration.</returns> public XInt GetPrimorial(int low, int high, int increment) { if (increment == 1) { return(this.GetPrimorial(new PositiveRange(low, high))); } var range = new PositiveRange(low, high); var pc = new PrimeCollection(this, range, increment); int start, size; if (pc.GetSliceParameters(out start, out size)) { return(XInt.One); } return(XMath.Product(this.primes, start, size, increment)); }
/// <summary> /// Initializes a collection consisting out of a single value. /// An integer cand is prime iff there is a prime number /// in the range (cand-1, cand]. /// </summary> /// <param name="sieve">The sieve to be used.</param> /// <param name="cand">The prime number candidate.</param> public PrimeCollection(PrimeSieve sieve, int cand) { // Note, that this PrimeCollection is not made public // via PrimeSieve. It is used only to test primality. // It is a private constructor only for PrimeSieve. this.sieve = sieve; this.primeRange = this.enumRange = null; this.start = this.IndexOf(cand - 1, 0, sieve.numberOfPrimes); this.end = sieve.primes[this.start] == cand ? this.start + 1 : this.start; this.isPrime = this.start < this.end; if (this.isPrime) { this.start = this.IndexOf(cand, this.start, sieve.numberOfPrimes); } this.nextPrime = sieve.primes[this.start]; this.increment = 0; }
/// <summary /> /// <param name="sieve"></param> /// <param name="enumRange"></param> /// <param name="increment"></param> public PrimeCollection(PrimeSieve sieve, PositiveRange enumRange, int increment) { sieve.sieveRange.ContainsOrFail(enumRange); this.sieve = sieve; this.enumRange = enumRange; int nops = sieve.numberOfPrimes; this.start = this.IndexOf(enumRange.Min - 1, 0, nops - 1); this.end = this.IndexOf(enumRange.Max, this.start, nops) - 1; if (this.end < this.start) //-- PrimeRange is empty. { this.end = -1; this.primeRange = new PositiveRange(0, 0); } else { this.primeRange = new PositiveRange(this.start + 1, this.end + 1); } this.increment = increment; }
/// <summary> /// Gives the collection of the prime numbers in the given range. /// </summary> /// <param name="range">The range of the collection.</param> /// <returns>The prime number collection.</returns> public IPrimeCollection GetPrimeCollection(PositiveRange range) { return(new PrimeCollection(this, range)); }
public TwoDimensionalPositiveRange(uint i_MinX, uint i_MaxX, uint i_MinY, uint i_MaxY) { m_RangeX = new PositiveRange(i_MinX, i_MaxX); m_RangeY = new PositiveRange(i_MinY, i_MaxY); }
// Get a positive number from the user, in 'i_InputRange' range public static uint GetPositiveNumberFromUser(string i_MessageForUser, PositiveRange i_InputRange) { return((uint)GetPositiveNumberFromUser(i_MessageForUser, i_InputRange, null)); }