Abundant odd numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (duplicate task)
(Added Go)
Line 4: Line 4:


{{alertbox|yellow|Duplicate of [[Abundant,_deficient_and_perfect_number_classifications]]}}
{{alertbox|yellow|Duplicate of [[Abundant,_deficient_and_perfect_number_classifications]]}}

=={{header|Go}}==
<lang go>package main

import (
"fmt"
"strconv"
)

func divisors(n int) []int {
divs := []int{1}
divs2 := []int{}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs = append(divs, i)
if i != j {
divs2 = append(divs2, j)
}
}
}
for i := len(divs2) - 1; i >= 0; i-- {
divs = append(divs, divs2[i])
}
return divs
}

func sum(divs []int) int {
tot := 0
for _, div := range divs {
tot += div
}
return tot
}

func sumStr(divs []int) string {
s := ""
for _, div := range divs {
s += strconv.Itoa(div) + " + "
}
return s[0 : len(s)-3]
}

func main() {
const max = 25
const maxOdd = 5
fmt.Println("The first", max, "nice numbers are:")
count := 0
for n := 1; count < max; n++ {
divs := divisors(n)
if tot := sum(divs); tot > n {
count++
s := sumStr(divs)
fmt.Printf("%2d. %3d => %s = %d > %d\n", count, n, s, tot, n)
}
}
fmt.Println("\nThe first", maxOdd, "odd nice numbers are:")
count = 0
for n := 1; count < maxOdd; n += 2 {
divs := divisors(n)
if tot := sum(divs); tot > n {
count++
s := sumStr(divs)
fmt.Printf("%d. %4d => %s = %d > %d\n", count, n, s, tot, n)
}
}
}</lang>

{{out}}
<pre>
The first 25 nice numbers are:
1. 12 => 1 + 2 + 3 + 4 + 6 = 16 > 12
2. 18 => 1 + 2 + 3 + 6 + 9 = 21 > 18
3. 20 => 1 + 2 + 4 + 5 + 10 = 22 > 20
4. 24 => 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36 > 24
5. 30 => 1 + 2 + 3 + 5 + 6 + 10 + 15 = 42 > 30
6. 36 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 > 36
7. 40 => 1 + 2 + 4 + 5 + 8 + 10 + 20 = 50 > 40
8. 42 => 1 + 2 + 3 + 6 + 7 + 14 + 21 = 54 > 42
9. 48 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 = 76 > 48
10. 54 => 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66 > 54
11. 56 => 1 + 2 + 4 + 7 + 8 + 14 + 28 = 64 > 56
12. 60 => 1 + 2 + 3 + 4 + 5 + 6 + 10 + 12 + 15 + 20 + 30 = 108 > 60
13. 66 => 1 + 2 + 3 + 6 + 11 + 22 + 33 = 78 > 66
14. 70 => 1 + 2 + 5 + 7 + 10 + 14 + 35 = 74 > 70
15. 72 => 1 + 2 + 3 + 4 + 6 + 8 + 9 + 12 + 18 + 24 + 36 = 123 > 72
16. 78 => 1 + 2 + 3 + 6 + 13 + 26 + 39 = 90 > 78
17. 80 => 1 + 2 + 4 + 5 + 8 + 10 + 16 + 20 + 40 = 106 > 80
18. 84 => 1 + 2 + 3 + 4 + 6 + 7 + 12 + 14 + 21 + 28 + 42 = 140 > 84
19. 88 => 1 + 2 + 4 + 8 + 11 + 22 + 44 = 92 > 88
20. 90 => 1 + 2 + 3 + 5 + 6 + 9 + 10 + 15 + 18 + 30 + 45 = 144 > 90
21. 96 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 + 32 + 48 = 156 > 96
22. 100 => 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 > 100
23. 102 => 1 + 2 + 3 + 6 + 17 + 34 + 51 = 114 > 102
24. 104 => 1 + 2 + 4 + 8 + 13 + 26 + 52 = 106 > 104
25. 108 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 + 27 + 36 + 54 = 172 > 108

The first 5 odd nice numbers are:
1. 945 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975 > 945
2. 1575 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 25 + 35 + 45 + 63 + 75 + 105 + 175 + 225 + 315 + 525 = 1649 > 1575
3. 2205 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 35 + 45 + 49 + 63 + 105 + 147 + 245 + 315 + 441 + 735 = 2241 > 2205
4. 2835 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 81 + 105 + 135 + 189 + 315 + 405 + 567 + 945 = 2973 > 2835
5. 3465 => 1 + 3 + 5 + 7 + 9 + 11 + 15 + 21 + 33 + 35 + 45 + 55 + 63 + 77 + 99 + 105 + 165 + 231 + 315 + 385 + 495 + 693 + 1155 = 4023 > 3465
</pre>


=={{header|REXX}}==
=={{header|REXX}}==

Revision as of 12:48, 17 May 2019

Abundant odd numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

n is Nice number if its sum of the factors is greater than n.

Duplicate of Abundant,_deficient_and_perfect_number_classifications

Go

<lang go>package main

import (

   "fmt"
   "strconv"

)

func divisors(n int) []int {

   divs := []int{1}
   divs2 := []int{}
   for i := 2; i*i <= n; i++ {
       if n%i == 0 {
           j := n / i
           divs = append(divs, i)
           if i != j {
               divs2 = append(divs2, j)
           }
       }
   }
   for i := len(divs2) - 1; i >= 0; i-- {
       divs = append(divs, divs2[i])
   }
   return divs

}

func sum(divs []int) int {

   tot := 0
   for _, div := range divs {
       tot += div
   }
   return tot

}

func sumStr(divs []int) string {

   s := ""
   for _, div := range divs {
       s += strconv.Itoa(div) + " + "
   }
   return s[0 : len(s)-3]

}

func main() {

   const max = 25
   const maxOdd = 5
   fmt.Println("The first", max, "nice numbers are:")
   count := 0
   for n := 1; count < max; n++ {
       divs := divisors(n)
       if tot := sum(divs); tot > n {
           count++
           s := sumStr(divs)
           fmt.Printf("%2d. %3d => %s = %d > %d\n", count, n, s, tot, n)
       }
   }
   fmt.Println("\nThe first", maxOdd, "odd nice numbers are:")
   count = 0
   for n := 1; count < maxOdd; n += 2 {
       divs := divisors(n)
       if tot := sum(divs); tot > n {
           count++
           s := sumStr(divs)
           fmt.Printf("%d. %4d => %s = %d > %d\n", count, n, s, tot, n)
       }
   }

}</lang>

Output:
The first 25 nice numbers are:
 1.  12 => 1 + 2 + 3 + 4 + 6 = 16 > 12
 2.  18 => 1 + 2 + 3 + 6 + 9 = 21 > 18
 3.  20 => 1 + 2 + 4 + 5 + 10 = 22 > 20
 4.  24 => 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36 > 24
 5.  30 => 1 + 2 + 3 + 5 + 6 + 10 + 15 = 42 > 30
 6.  36 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 > 36
 7.  40 => 1 + 2 + 4 + 5 + 8 + 10 + 20 = 50 > 40
 8.  42 => 1 + 2 + 3 + 6 + 7 + 14 + 21 = 54 > 42
 9.  48 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 = 76 > 48
10.  54 => 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66 > 54
11.  56 => 1 + 2 + 4 + 7 + 8 + 14 + 28 = 64 > 56
12.  60 => 1 + 2 + 3 + 4 + 5 + 6 + 10 + 12 + 15 + 20 + 30 = 108 > 60
13.  66 => 1 + 2 + 3 + 6 + 11 + 22 + 33 = 78 > 66
14.  70 => 1 + 2 + 5 + 7 + 10 + 14 + 35 = 74 > 70
15.  72 => 1 + 2 + 3 + 4 + 6 + 8 + 9 + 12 + 18 + 24 + 36 = 123 > 72
16.  78 => 1 + 2 + 3 + 6 + 13 + 26 + 39 = 90 > 78
17.  80 => 1 + 2 + 4 + 5 + 8 + 10 + 16 + 20 + 40 = 106 > 80
18.  84 => 1 + 2 + 3 + 4 + 6 + 7 + 12 + 14 + 21 + 28 + 42 = 140 > 84
19.  88 => 1 + 2 + 4 + 8 + 11 + 22 + 44 = 92 > 88
20.  90 => 1 + 2 + 3 + 5 + 6 + 9 + 10 + 15 + 18 + 30 + 45 = 144 > 90
21.  96 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 + 32 + 48 = 156 > 96
22. 100 => 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 > 100
23. 102 => 1 + 2 + 3 + 6 + 17 + 34 + 51 = 114 > 102
24. 104 => 1 + 2 + 4 + 8 + 13 + 26 + 52 = 106 > 104
25. 108 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 + 27 + 36 + 54 = 172 > 108

The first 5 odd nice numbers are:
1.  945 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975 > 945
2. 1575 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 25 + 35 + 45 + 63 + 75 + 105 + 175 + 225 + 315 + 525 = 1649 > 1575
3. 2205 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 35 + 45 + 49 + 63 + 105 + 147 + 245 + 315 + 441 + 735 = 2241 > 2205
4. 2835 => 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 81 + 105 + 135 + 189 + 315 + 405 + 567 + 945 = 2973 > 2835
5. 3465 => 1 + 3 + 5 + 7 + 9 + 11 + 15 + 21 + 33 + 35 + 45 + 55 + 63 + 77 + 99 + 105 + 165 + 231 + 315 + 385 + 495 + 693 + 1155 = 4023 > 3465

REXX

<lang rexx>/*REXX pgm displays N nice numbers, where nice #'s are whose sum of its factors > #. */ parse arg N . /*obtain optional arguments from the CL*/ if N== | N=="," then N= 25 /*Not specified? Then use the default.*/ w= length(N); ww= w+ w /*used for aligning the columnar output*/

  1. = 0 /*count of nice numbers (so far). */
     do j=1  until #>=N;     $=sigma(j)         /*get sigma for an integer.            */
     if $<=j  then iterate                      /*sigma  +  J ?    Then ignore it.     */
     #= # + 1
     say 'nice number '   right(#,w)   " is:"   right(j,ww)',     sigma is:'  right($,ww)
     end  /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ sigma: procedure; parse arg x; if x<2 then return 0; odd= x // 2 /* // ◄──remainder.*/

      s= 1                                      /* [↓]  only use  EVEN or ODD integers.*/
            do j=2+odd  by 1+odd  while j*j<x   /*divide by all integers up to  √x.    */
            if x//j==0  then  s= s + j +  x%j   /*add the two divisors to (sigma) sum. */
            end   /*j*/                         /* [↑]  %  is the REXX integer division*/
                                                /* [↓]  adjust for a square.       ___ */
      if j*j==x  then  return s + j             /*Was  X  a square?   If so, add  √ x  */
                       return s                 /*return (sigma) sum of the divisors.  */</lang>
output   when using the default input:
nice number   1  is:   12,     sigma is:   16
nice number   2  is:   18,     sigma is:   21
nice number   3  is:   20,     sigma is:   22
nice number   4  is:   24,     sigma is:   36
nice number   5  is:   30,     sigma is:   42
nice number   6  is:   36,     sigma is:   55
nice number   7  is:   40,     sigma is:   50
nice number   8  is:   42,     sigma is:   54
nice number   9  is:   48,     sigma is:   76
nice number  10  is:   54,     sigma is:   66
nice number  11  is:   56,     sigma is:   64
nice number  12  is:   60,     sigma is:  108
nice number  13  is:   66,     sigma is:   78
nice number  14  is:   70,     sigma is:   74
nice number  15  is:   72,     sigma is:  123
nice number  16  is:   78,     sigma is:   90
nice number  17  is:   80,     sigma is:  106
nice number  18  is:   84,     sigma is:  140
nice number  19  is:   88,     sigma is:   92
nice number  20  is:   90,     sigma is:  144
nice number  21  is:   96,     sigma is:  156
nice number  22  is:  100,     sigma is:  117
nice number  23  is:  102,     sigma is:  114
nice number  24  is:  104,     sigma is:  106
nice number  25  is:  108,     sigma is:  172

Ring

<lang ring>

  1. Project: Nice numbers

max = 25 nr = 0 m = 1 check = 0 index = 0 while true

     nice(m)
     if check = 1
        nr = nr + 1
     ok
     if nr = max
        exit
     ok
     m = m + 1

end

func nice(n)

    check = 0
    nArray = []
    for i = 1 to n - 1
        if n % i = 0
           add(nArray,i)
        ok
    next
    sum = 0
    for p = 1 to len(nArray)
        sum = sum + nArray[p]
    next
    if sum > n
       check = 1
       index = index + 1
       showArray(n,nArray,sum,index)
    ok

func showArray(n,nArray,sum,index)

    see string(index) + ". " + string(n) + " => "
    for m = 1 to len(nArray)
        if m < len(nArray)
           see string(nArray[m]) + " + "
        else
           see string(nArray[m]) + " = " + string(sum) + " > " + string(n) + nl
        ok
    next

</lang>

Output:
1. 12 => 1 + 2 + 3 + 4 + 6 = 16 > 12
2. 18 => 1 + 2 + 3 + 6 + 9 = 21 > 18
3. 20 => 1 + 2 + 4 + 5 + 10 = 22 > 20
4. 24 => 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36 > 24
5. 30 => 1 + 2 + 3 + 5 + 6 + 10 + 15 = 42 > 30
6. 36 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 > 36
7. 40 => 1 + 2 + 4 + 5 + 8 + 10 + 20 = 50 > 40
8. 42 => 1 + 2 + 3 + 6 + 7 + 14 + 21 = 54 > 42
9. 48 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 = 76 > 48
10. 54 => 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66 > 54
11. 56 => 1 + 2 + 4 + 7 + 8 + 14 + 28 = 64 > 56
12. 60 => 1 + 2 + 3 + 4 + 5 + 6 + 10 + 12 + 15 + 20 + 30 = 108 > 60
13. 66 => 1 + 2 + 3 + 6 + 11 + 22 + 33 = 78 > 66
14. 70 => 1 + 2 + 5 + 7 + 10 + 14 + 35 = 74 > 70
15. 72 => 1 + 2 + 3 + 4 + 6 + 8 + 9 + 12 + 18 + 24 + 36 = 123 > 72
16. 78 => 1 + 2 + 3 + 6 + 13 + 26 + 39 = 90 > 78
17. 80 => 1 + 2 + 4 + 5 + 8 + 10 + 16 + 20 + 40 = 106 > 80
18. 84 => 1 + 2 + 3 + 4 + 6 + 7 + 12 + 14 + 21 + 28 + 42 = 140 > 84
19. 88 => 1 + 2 + 4 + 8 + 11 + 22 + 44 = 92 > 88
20. 90 => 1 + 2 + 3 + 5 + 6 + 9 + 10 + 15 + 18 + 30 + 45 = 144 > 90
21. 96 => 1 + 2 + 3 + 4 + 6 + 8 + 12 + 16 + 24 + 32 + 48 = 156 > 96
22. 100 => 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 > 100
23. 102 => 1 + 2 + 3 + 6 + 17 + 34 + 51 = 114 > 102
24. 104 => 1 + 2 + 4 + 8 + 13 + 26 + 52 = 106 > 104
25. 108 => 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 + 27 + 36 + 54 = 172 > 108