Exemplo n.º 1
0
        //    A number n is called called abundant if sum of its proper divisors exceeds n.
        public long GetNonabundantsum(int to)
        {
            //lag liste av aktuelle AbundantNumber
            var abundantNumbersListe = Enumerable.Range(1, to).Where(i => Divisors.IsAbundantNumber(i)).ToList();

            //lag hashmap av AbundantNumber listen
            var abundantNumbersDictionary = abundantNumbersListe.ToDictionary(e => e);



            //finn summen
            //er  i= a+b hvor a og b er AbundantNumber ? , ellers legg til sum
            long nonabundantsum = 0;


            for (int i = 1; i <= to; i++)
            {
                if (
                    abundantNumbersListe
                    .Where(e => e < i)
                    .Where(a => abundantNumbersDictionary.ContainsKey(i - a))
                    .FirstOrDefault() == 0     //hvis ikke i kan skrives som summen av to tall i abundantNumbersListe
                    )
                {
                    nonabundantsum += i;
                }
            }

            return(nonabundantsum);
        }
Exemplo n.º 2
0
        public void Test1()
        {
            /*
             * As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16,
             * the smallest number that can be written as the sum of two abundant numbers is 24.
             */

            Assert.True(Divisors.IsAbundantNumber(12));

            var  sut = new E023Nonabundantsums();
            long nonabundantsumExpected = 276;
            long nonabundantsum         = sut.GetNonabundantsum(to: 23);

            Assert.Equal(nonabundantsumExpected, nonabundantsum);

            //24 kan skrive som sum
            nonabundantsum = sut.GetNonabundantsum(to: 24);
            Assert.Equal(nonabundantsumExpected, nonabundantsum);
        }