Arithmetic numbers: Difference between revisions
(Created new draft task and added a Wren solution.) |
(Add Factor) |
||
Line 28: | Line 28: | ||
* [[oeis:A003601|OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer]] |
* [[oeis:A003601|OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer]] |
||
<br><br> |
<br><br> |
||
=={{header|Factor}}== |
|||
{{works with|Factor|0.99 2022-04-03}} |
|||
<lang factor>USING: combinators formatting grouping io kernel lists |
|||
lists.lazy math math.functions math.primes math.primes.factors |
|||
math.statistics math.text.english prettyprint sequences |
|||
tools.memory.private ; |
|||
: arith? ( n -- ? ) divisors mean integer? ; |
|||
: larith ( -- list ) 1 lfrom [ arith? ] lfilter ; |
|||
: arith ( m -- seq ) larith ltake list>array ; |
|||
: composite? ( n -- ? ) dup 1 > swap prime? not and ; |
|||
: ordinal ( n -- str ) [ commas ] keep ordinal-suffix append ; |
|||
: info. ( n -- ) |
|||
{ |
|||
[ ordinal "%s arithmetic number: " printf ] |
|||
[ arith dup last commas print ] |
|||
[ commas "Number of composite arithmetic numbers <= %s: " printf ] |
|||
[ drop [ composite? ] count commas print nl ] |
|||
} cleave ; |
|||
"First 100 arithmetic numbers:" print |
|||
100 arith 10 group simple-table. nl |
|||
{ 3 4 5 6 } [ 10^ info. ] each</lang> |
|||
{{out}} |
|||
<pre> |
|||
First 100 arithmetic numbers: |
|||
1 3 5 6 7 11 13 14 15 17 |
|||
19 20 21 22 23 27 29 30 31 33 |
|||
35 37 38 39 41 42 43 44 45 46 |
|||
47 49 51 53 54 55 56 57 59 60 |
|||
61 62 65 66 67 68 69 70 71 73 |
|||
77 78 79 83 85 86 87 89 91 92 |
|||
93 94 95 96 97 99 101 102 103 105 |
|||
107 109 110 111 113 114 115 116 118 119 |
|||
123 125 126 127 129 131 132 133 134 135 |
|||
137 138 139 140 141 142 143 145 147 149 |
|||
1,000th arithmetic number: 1,361 |
|||
Number of composite arithmetic numbers <= 1,000: 782 |
|||
10,000th arithmetic number: 12,953 |
|||
Number of composite arithmetic numbers <= 10,000: 8,458 |
|||
100,000th arithmetic number: 125,587 |
|||
Number of composite arithmetic numbers <= 100,000: 88,219 |
|||
1,000,000th arithmetic number: 1,228,663 |
|||
Number of composite arithmetic numbers <= 1,000,000: 905,043 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
Revision as of 10:59, 13 June 2022
- Definition
A positive integer n is an arithmetic number if the average of its positive divisors is also an integer.
Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5.
- Example
30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer.
- Task
Calculate and show here:
1. The first 100 arithmetic numbers.
2. The xth arithmetic number where x = 1,000 and x = 10,000.
3. How many arithmetic numbers <= x are composite.
Note that, technically, the arithmetic number 1 is neither prime nor composite.
- Stretch
Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000.
- References
- Wikipedia: Arithmetic number
- OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
Factor
<lang factor>USING: combinators formatting grouping io kernel lists lists.lazy math math.functions math.primes math.primes.factors math.statistics math.text.english prettyprint sequences tools.memory.private ;
- arith? ( n -- ? ) divisors mean integer? ;
- larith ( -- list ) 1 lfrom [ arith? ] lfilter ;
- arith ( m -- seq ) larith ltake list>array ;
- composite? ( n -- ? ) dup 1 > swap prime? not and ;
- ordinal ( n -- str ) [ commas ] keep ordinal-suffix append ;
- info. ( n -- )
{ [ ordinal "%s arithmetic number: " printf ] [ arith dup last commas print ] [ commas "Number of composite arithmetic numbers <= %s: " printf ] [ drop [ composite? ] count commas print nl ] } cleave ;
"First 100 arithmetic numbers:" print
100 arith 10 group simple-table. nl
{ 3 4 5 6 } [ 10^ info. ] each</lang>
- Output:
First 100 arithmetic numbers: 1 3 5 6 7 11 13 14 15 17 19 20 21 22 23 27 29 30 31 33 35 37 38 39 41 42 43 44 45 46 47 49 51 53 54 55 56 57 59 60 61 62 65 66 67 68 69 70 71 73 77 78 79 83 85 86 87 89 91 92 93 94 95 96 97 99 101 102 103 105 107 109 110 111 113 114 115 116 118 119 123 125 126 127 129 131 132 133 134 135 137 138 139 140 141 142 143 145 147 149 1,000th arithmetic number: 1,361 Number of composite arithmetic numbers <= 1,000: 782 10,000th arithmetic number: 12,953 Number of composite arithmetic numbers <= 10,000: 8,458 100,000th arithmetic number: 125,587 Number of composite arithmetic numbers <= 100,000: 88,219 1,000,000th arithmetic number: 1,228,663 Number of composite arithmetic numbers <= 1,000,000: 905,043
Wren
<lang ecmascript>import "./math" for Int, Nums import "./fmt" for Fmt import "./sort" for Find
var arithmetic = [1] var primes = [] var limit = 1e6 var n = 3 while (arithmetic.count < limit) {
var divs = Int.divisors(n) if (divs.count == 2) { primes.add(n) arithmetic.add(n) } else { var mean = Nums.mean(divs) if (mean.isInteger) arithmetic.add(n) } n = n + 1
} System.print("The first 100 arithmetic numbers are:") Fmt.tprint("$3d", arithmetic[0..99], 10)
for (x in [1e3, 1e4, 1e5, 1e6]) {
var last = arithmetic[x-1] Fmt.print("\nThe $,dth arithmetic number is: $,d", x, last) var pcount = Find.nearest(primes, last) + 1 if (!Int.isPrime(last)) pcount = pcount - 1 var comp = x - pcount - 1 // 1 is not composite Fmt.print("The count of such numbers <= $,d which are composite is $,d.", last, comp)
}</lang>
- Output:
The first 100 arithmetic numbers are: 1 3 5 6 7 11 13 14 15 17 19 20 21 22 23 27 29 30 31 33 35 37 38 39 41 42 43 44 45 46 47 49 51 53 54 55 56 57 59 60 61 62 65 66 67 68 69 70 71 73 77 78 79 83 85 86 87 89 91 92 93 94 95 96 97 99 101 102 103 105 107 109 110 111 113 114 115 116 118 119 123 125 126 127 129 131 132 133 134 135 137 138 139 140 141 142 143 145 147 149 The 1,000th arithmetic number is: 1,361 The count of such numbers <= 1,361 which are composite is 782. The 10,000th arithmetic number is: 12,953 The count of such numbers <= 12,953 which are composite is 8,458. The 100,000th arithmetic number is: 125,587 The count of such numbers <= 125,587 which are composite is 88,219. The 1,000,000th arithmetic number is: 1,228,663 The count of such numbers <= 1,228,663 which are composite is 905,043.