using System; using System.Collections.Generic; namespace A290869 { /// <summary> /// Program for A290869. /// Numbers that are repdigits with length > 2 in more that two bases. /// </summary> class Program { /// <summary> /// Generate A290869. /// </summary> static void Main() { try { int n = 0; long value = 0; int valueCount = 0; while (true) { long next = Pop(); if (value == next) { valueCount++; if (valueCount == 3) { n++; Console.WriteLine("{0} {1}", n, value); if (n==500) { break; } } } else { value = next; valueCount = 1; } } } catch (Exception x) { Console.WriteLine("Ouch {0}", x.Message); } } /// <summary> /// Pop the next repdigit. /// </summary> /// <returns></returns> static long Pop() { if (RepDigits.Count==0) { RepDigits.Add(RepUnit); RepUnit = new RepDigit(RepUnit.Base + 1); } RepDigit min = RepDigits.Min; RepDigits.Remove(min); RepDigit next = min.Next; RepDigits.Add(next); while (RepUnit.Value <= next.Value) { RepDigits.Add(RepUnit); RepUnit = new RepDigit(RepUnit.Base + 1); } return min.Value; } /// <summary> /// Repdigits with at least 3 digits. /// </summary> static SortedSet<RepDigit> RepDigits = new SortedSet<RepDigit>(); /// <summary> /// Next unseen repunit. /// </summary> static RepDigit RepUnit = new RepDigit(2); } /// <summary> /// Repdigit with at least 3 digits. /// </summary> class RepDigit : IComparable, IComparable<RepDigit> { /// <summary> /// First 3-digit repdigit from given base. /// </summary> /// <param name="base">base</param> public RepDigit(long @base) { checked { this.Base = @base; this.Repunit = 1 + @base + @base * @base; this.Digit = 1; this.Value = this.Repunit; } } /// <summary> /// Base. /// </summary> public long Base { get; private set; } /// <summary> /// Underlying repunit. /// </summary> public long Repunit { get; private set; } /// <summary> /// Digit (Value = Repunit * Digit). /// </summary> public long Digit { get; private set; } /// <summary> /// Value. /// </summary> public long Value { get; private set; } /// <summary> /// Next repdigit for the same base. /// </summary> public RepDigit Next { get { RepDigit next = new RepDigit(); checked { next.Base = this.Base; next.Repunit = this.Repunit; next.Digit = this.Digit + 1; if (next.Digit == next.Base) { next.Digit = 1; next.Repunit *= next.Base; next.Repunit += 1; next.Value = next.Repunit; } else { next.Value = this.Value + this.Repunit; } } return next; } } /// <summary> /// Compares this object with an other. /// </summary> /// <param name="obj">other</param> /// <returns>comparison result</returns> public int CompareTo(object obj) { return this.CompareTo(obj as RepDigit); } /// <summary> /// Compares this object with an other. /// </summary> /// <param name="other">other</param> /// <returns>comparison result</returns> public int CompareTo(RepDigit other) { int c = this.Value.CompareTo(other.Value); if (c == 0) { c = this.Base.CompareTo(other.Base); } return c; } /// <summary> /// Create a new instance. /// </summary> private RepDigit() { } } }