/// <summary> /// Gets a linked list containing all the primes less than n. /// </summary> /// <param name="n">The upper bound on the primes (all primes must be less than this value).</param> /// <returns>A linked list containing all the primes less than n.</returns> private LinkedListCell <int> GetPrimesLessThan(int n) { LinkedListCell <int> list = GetNumbersLessThan(n); for (LinkedListCell <int> p = list; p != null && p.Data * p.Data < n; p = p.Next) { RemoveMultiples(p.Data, p); } return(list); }
/// <summary> /// Gets a linked list of the prime numbers less than the given value. /// </summary> /// <param name="n">The upper limit.</param> /// <returns>A list of the prime numbers less than n.</returns> public static LinkedListCell <int> GetPrimesLessThan(int n) { LinkedListCell <int> list = GetNumbersLessThan(n); LinkedListCell <int> p = list; while (p != null && p.Data * p.Data < n) { RemoveMultiples(p.Data, p); p = p.Next; } return(list); }
/// <summary> /// Forms a linked list containing all prime numbers less than the given value. /// </summary> /// <param name="value">The upper limit (exclusive) on the prime numbers to generate.</param> /// <returns>A linked list containing all the prime numbers less than value.</returns> private LinkedListCell <int> GetPrimesLessThan(int value) { LinkedListCell <int> x = GetNumbersLessThan(value); LinkedListCell <int> temp = x; while (temp != null) { RemoveMultiples(temp.Data, temp); temp = temp.Next; } return(x); }
/// <summary> /// Handles a Click event on the "Find Primes" button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxFindPrimes_Click(object sender, EventArgs e) { LinkedListCell <int> primes = GetPrimesLessThan((int)uxInput.Value); uxPrimes.BeginUpdate(); uxPrimes.Items.Clear(); for (LinkedListCell <int> p = primes; p != null; p = p.Next) { uxPrimes.Items.Add(p.Data); } uxPrimes.EndUpdate(); }
/// <summary> /// Forms a linked list containing all prime numbers less than the given value. /// </summary> /// <param name="value">The upper limit (exclusive) on the prime numbers to generate.</param> /// <returns>A linked list containing all the prime numbers less than value.</returns> private LinkedListCell <int> GetPrimesLessThan(int value) { LinkedListCell <int> tempCell = GetNumbersLessThan(value); LinkedListCell <int> pointer = tempCell; while (pointer != null) { RemoveMultiples(pointer.Data, pointer); pointer = pointer.Next; } return(tempCell); }//end GetPrimesLessThan
/// <summary> /// Gets a linked list of the integers strictly between 1 and n. /// </summary> /// <param name="n">The upper limit.</param> /// <returns>A list of the integers strictly between 1 and n.</returns> public static LinkedListCell <int> GetNumbersLessThan(int n) { LinkedListCell <int> list = null; for (int i = n - 1; i > 1; i--) { LinkedListCell <int> cell = new LinkedListCell <int>(); cell.Data = i; cell.Next = list; list = cell; } return(list); }
/// <summary> /// Returns a linked list beginning at 2 and is less than n /// </summary> /// <param name="n"></param> /// <returns></returns> private LinkedListCell <int> GetNumbersLessThan(int n) { LinkedListCell <int> temp = new LinkedListCell <int>(); for (int i = n - 1; i > 1; i--) { LinkedListCell <int> cell = new LinkedListCell <int>(); cell.Data = i; cell.Next = temp; temp = cell; } return(temp); }
/// <summary> /// Gets a linked list containing all the primes less than n. /// </summary> /// <param name="n">The upper bound on the primes (all primes must be less than this value).</param> /// <returns>A linked list containing all the primes less than n.</returns> private LinkedListCell <int> GetPrimesLessThan(int n) { LinkedListCell <int> front = GetNumbersLessThan(n); LinkedListCell <int> temp = front; while (temp.Next != null && (temp.Data * temp.Data < n)) { RemoveMultiples(temp.Data, temp); temp = temp.Next; } return(front); }
/// <summary> /// Gets a linked list containing the values 2 through n - 1. /// </summary> /// <param name="n">One greater than the largest value to be placed in the list.</param> /// <returns>A linked list containing the values 2 through n - 1.</returns> private LinkedListCell <int> GetNumbersLessThan(int n) { LinkedListCell <int> front = null; for (int i = n - 1; i > 1; i--) { LinkedListCell <int> temp = new LinkedListCell <int>(); temp.Data = i; temp.Next = front; front = temp; } return(front); }
}//end GetPrimesLessThan /// <summary> /// Private method GetNumbersLessThan; Returns LinkedListCell<int>; /// </summary> /// <param name="n"></param> /// <returns></returns> private LinkedListCell <int> GetNumbersLessThan(int n) { LinkedListCell <int> numLessThan = null; for (int i = n - 1; i > 1; i--) { LinkedListCell <int> tempCell = new LinkedListCell <int>(); tempCell.Data = i; tempCell.Next = numLessThan; numLessThan = tempCell; } //end for return(numLessThan); } //end LinkedListCell
/// <summary> /// Removes all multiples of the given int from the linked list beginning after the given cell. /// </summary> /// <param name="k">The value whose multiples are to be removed.</param> /// <param name="list">The cell preceding the first cell to examine.</param> public static void RemoveMultiples(int k, LinkedListCell <int> list) { while (list.Next != null) { if (list.Next.Data % k == 0) { list.Next = list.Next.Next; } else { list = list.Next; } } }
/// <summary> /// Removes from the given linked list all cells (except the first) containing /// values divisible by the given int. The given cell must not be null. /// </summary> /// <param name="k">The value whose multiples are to be removed.</param> /// <param name="list">The cell containing the divisor.</param> private void RemoveMultiples(int k, LinkedListCell <int> list) { LinkedListCell <int> temp = list; while (temp.Next != null) { if (temp.Next.Data % k == 0) { temp.Next = temp.Next.Next; } else { temp = temp.Next; } } }
} //end LinkedListCell /// <summary> /// Private method RemoveMultiples; returns null /// </summary> /// <param name="k"></param> /// <param name="list"></param> private void RemoveMultiples(int k, LinkedListCell <int> list) { LinkedListCell <int> pointer = list; while (pointer.Next != null) { if ((pointer.Next.Data % k) == 0) { pointer.Next = pointer.Next.Next; } else { pointer = pointer.Next; } } //end while } //RemoveMultiples