Multiplicatively perfect numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (put notice of duplicate task)
Line 15: Line 15:
<syntaxhighlight lang="ring">
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
see "Special numbers under 500:" + nl
limit = 500
limit = 500
Divisors = []
Divisors = []
Line 20: Line 21:
pro = 1
pro = 1
Divisors = []
Divisors = []
for m = 2 to ceil(sqrt(n))+1
for m = 2 to ceil(n/2)
if n % m = 0
if n % m = 0
pro = pro * m
pro = pro * m
Line 29: Line 30:
if n = pro and len(Divisors) > 1
if n = pro and len(Divisors) > 1
for m = 1 to len(Divisors)
for m = 1 to len(Divisors)
str = str + Divisors[m] + ", "
str = str + Divisors[m] + " * "
if m = len(Divisors)
if m = len(Divisors)
str = left(str,len(str)-2) + "]"
str = left(str,len(str)-2)
ok
ok
next
next
see "n = " + n + " divisors = " + "[" + str + " product = " + pro + nl
see "" + n + " = " + str + nl
ok
ok
next
next
Line 41: Line 42:
{{out}}
{{out}}
<pre>
<pre>
working...
working...Special numbers under 500:
n = 6 divisors = [2, 3] product = 6
6 = 2 x 3
n = 8 divisors = [2, 4] product = 8
8 = 2 x 4
n = 10 divisors = [2, 5] product = 10
10 = 2 x 5
n = 15 divisors = [3, 5] product = 15
14 = 2 x 7
n = 35 divisors = [5, 7] product = 35
15 = 3 x 5
n = 64 divisors = [2, 4, 8] product = 64
21 = 3 x 7
22 = 2 x 11
n = 105 divisors = [3, 5, 7] product = 105
26 = 2 x 13
n = 135 divisors = [3, 5, 9] product = 135
27 = 3 x 9
n = 143 divisors = [11, 13] product = 143
n = 165 divisors = [3, 5, 11] product = 165
33 = 3 x 11
34 = 2 x 17
n = 189 divisors = [3, 7, 9] product = 189
n = 231 divisors = [3, 7, 11] product = 231
35 = 5 x 7
38 = 2 x 19
n = 273 divisors = [3, 7, 13] product = 273
n = 286 divisors = [2, 11, 13] product = 286
39 = 3 x 13
46 = 2 x 23
n = 297 divisors = [3, 9, 11] product = 297
51 = 3 x 17
n = 323 divisors = [17, 19] product = 323
55 = 5 x 11
n = 351 divisors = [3, 9, 13] product = 351
57 = 3 x 19
n = 357 divisors = [3, 7, 17] product = 357
58 = 2 x 29
n = 374 divisors = [2, 11, 17] product = 374
62 = 2 x 31
n = 385 divisors = [5, 7, 11] product = 385
n = 429 divisors = [3, 11, 13] product = 429
65 = 5 x 13
69 = 3 x 23
n = 442 divisors = [2, 13, 17] product = 442
74 = 2 x 37
n = 455 divisors = [5, 7, 13] product = 455
77 = 7 x 11
n = 459 divisors = [3, 9, 17] product = 459
82 = 2 x 41
n = 494 divisors = [2, 13, 19] product = 494
85 = 5 x 17
86 = 2 x 43
87 = 3 x 29
91 = 7 x 13
93 = 3 x 31
94 = 2 x 47
95 = 5 x 19
106 = 2 x 53
111 = 3 x 37
115 = 5 x 23
118 = 2 x 59
119 = 7 x 17
122 = 2 x 61
123 = 3 x 41
125 = 5 x 25
129 = 3 x 43
133 = 7 x 19
134 = 2 x 67
141 = 3 x 47
142 = 2 x 71
143 = 11 x 13
145 = 5 x 29
146 = 2 x 73
155 = 5 x 31
158 = 2 x 79
159 = 3 x 53
161 = 7 x 23
166 = 2 x 83
177 = 3 x 59
178 = 2 x 89
183 = 3 x 61
185 = 5 x 37
187 = 11 x 17
194 = 2 x 97
201 = 3 x 67
202 = 2 x 101
203 = 7 x 29
205 = 5 x 41
206 = 2 x 103
209 = 11 x 19
213 = 3 x 71
214 = 2 x 107
215 = 5 x 43
217 = 7 x 31
218 = 2 x 109
219 = 3 x 73
221 = 13 x 17
226 = 2 x 113
235 = 5 x 47
237 = 3 x 79
247 = 13 x 19
249 = 3 x 83
253 = 11 x 23
254 = 2 x 127
259 = 7 x 37
262 = 2 x 131
265 = 5 x 53
267 = 3 x 89
274 = 2 x 137
278 = 2 x 139
287 = 7 x 41
291 = 3 x 97
295 = 5 x 59
298 = 2 x 149
299 = 13 x 23
301 = 7 x 43
302 = 2 x 151
303 = 3 x 101
305 = 5 x 61
309 = 3 x 103
314 = 2 x 157
319 = 11 x 29
321 = 3 x 107
323 = 17 x 19
326 = 2 x 163
327 = 3 x 109
329 = 7 x 47
334 = 2 x 167
335 = 5 x 67
339 = 3 x 113
341 = 11 x 31
343 = 7 x 49
346 = 2 x 173
355 = 5 x 71
358 = 2 x 179
362 = 2 x 181
365 = 5 x 73
371 = 7 x 53
377 = 13 x 29
381 = 3 x 127
382 = 2 x 191
386 = 2 x 193
391 = 17 x 23
393 = 3 x 131
394 = 2 x 197
395 = 5 x 79
398 = 2 x 199
403 = 13 x 31
407 = 11 x 37
411 = 3 x 137
413 = 7 x 59
415 = 5 x 83
417 = 3 x 139
422 = 2 x 211
427 = 7 x 61
437 = 19 x 23
445 = 5 x 89
446 = 2 x 223
447 = 3 x 149
451 = 11 x 41
453 = 3 x 151
454 = 2 x 227
458 = 2 x 229
466 = 2 x 233
469 = 7 x 67
471 = 3 x 157
473 = 11 x 43
478 = 2 x 239
481 = 13 x 37
482 = 2 x 241
485 = 5 x 97
489 = 3 x 163
493 = 17 x 29
497 = 7 x 71
done...
done...
</pre>
</pre>

Revision as of 05:07, 20 April 2023

Duplicate of task Semiprime

Definition

If the product of the divisors of an integer n (other than 1 and n itself) is equal to the number itself, then n is a special number.
Task
Find and show on this page the Special numbers where n < 500


This example is incorrect. Please fix the code and remove this message.

Details: program doesn't find all divisors for n

Ring

see "working..." + nl
see "Special numbers under 500:" + nl
limit = 500
Divisors = []
for n = 1 to limit
    pro = 1
    Divisors = []
    for m = 2 to ceil(n/2)
        if n % m = 0
           pro = pro * m
           add(Divisors,m)
        ok
    next
    str = ""
    if n = pro and len(Divisors) > 1
       for m = 1 to len(Divisors)
           str = str + Divisors[m] + " * "
           if m = len(Divisors)
              str = left(str,len(str)-2) 
           ok
       next
       see "" + n + " = " + str + nl
    ok
next
see "done..." + nl
Output:
working...Special numbers under 500:
  6  =   2 x   3
  8  =   2 x   4
 10  =   2 x   5
 14  =   2 x   7
 15  =   3 x   5
 21  =   3 x   7
 22  =   2 x  11
 26  =   2 x  13
 27  =   3 x   9
 33  =   3 x  11
 34  =   2 x  17
 35  =   5 x   7
 38  =   2 x  19
 39  =   3 x  13
 46  =   2 x  23
 51  =   3 x  17
 55  =   5 x  11
 57  =   3 x  19
 58  =   2 x  29
 62  =   2 x  31
 65  =   5 x  13
 69  =   3 x  23
 74  =   2 x  37
 77  =   7 x  11
 82  =   2 x  41
 85  =   5 x  17
 86  =   2 x  43
 87  =   3 x  29
 91  =   7 x  13
 93  =   3 x  31
 94  =   2 x  47
 95  =   5 x  19
106  =   2 x  53
111  =   3 x  37
115  =   5 x  23
118  =   2 x  59
119  =   7 x  17
122  =   2 x  61
123  =   3 x  41
125  =   5 x  25
129  =   3 x  43
133  =   7 x  19
134  =   2 x  67
141  =   3 x  47
142  =   2 x  71
143  =  11 x  13
145  =   5 x  29
146  =   2 x  73
155  =   5 x  31
158  =   2 x  79
159  =   3 x  53
161  =   7 x  23
166  =   2 x  83
177  =   3 x  59
178  =   2 x  89
183  =   3 x  61
185  =   5 x  37
187  =  11 x  17
194  =   2 x  97
201  =   3 x  67
202  =   2 x 101
203  =   7 x  29
205  =   5 x  41
206  =   2 x 103
209  =  11 x  19
213  =   3 x  71
214  =   2 x 107
215  =   5 x  43
217  =   7 x  31
218  =   2 x 109
219  =   3 x  73
221  =  13 x  17
226  =   2 x 113
235  =   5 x  47
237  =   3 x  79
247  =  13 x  19
249  =   3 x  83
253  =  11 x  23
254  =   2 x 127
259  =   7 x  37
262  =   2 x 131
265  =   5 x  53
267  =   3 x  89
274  =   2 x 137
278  =   2 x 139
287  =   7 x  41
291  =   3 x  97
295  =   5 x  59
298  =   2 x 149
299  =  13 x  23
301  =   7 x  43
302  =   2 x 151
303  =   3 x 101
305  =   5 x  61
309  =   3 x 103
314  =   2 x 157
319  =  11 x  29
321  =   3 x 107
323  =  17 x  19
326  =   2 x 163
327  =   3 x 109
329  =   7 x  47
334  =   2 x 167
335  =   5 x  67
339  =   3 x 113
341  =  11 x  31
343  =   7 x  49
346  =   2 x 173
355  =   5 x  71
358  =   2 x 179
362  =   2 x 181
365  =   5 x  73
371  =   7 x  53
377  =  13 x  29
381  =   3 x 127
382  =   2 x 191
386  =   2 x 193
391  =  17 x  23
393  =   3 x 131
394  =   2 x 197
395  =   5 x  79
398  =   2 x 199
403  =  13 x  31
407  =  11 x  37
411  =   3 x 137
413  =   7 x  59
415  =   5 x  83
417  =   3 x 139
422  =   2 x 211
427  =   7 x  61
437  =  19 x  23
445  =   5 x  89
446  =   2 x 223
447  =   3 x 149
451  =  11 x  41
453  =   3 x 151
454  =   2 x 227
458  =   2 x 229
466  =   2 x 233
469  =   7 x  67
471  =   3 x 157
473  =  11 x  43
478  =   2 x 239
481  =  13 x  37
482  =   2 x 241
485  =   5 x  97
489  =   3 x 163
493  =  17 x  29
497  =   7 x  71
done...

Wren

Library: Wren-math
Library: Wren-fmt

These are what are called 'multiplicatively perfect numbers' (see OEIS-A00742).

If this is intended to be a draft task, then I think the title should be changed to that.

import "./math" for Int, Nums
import "./fmt" for Fmt

var limit = 500
System.print("Special numbers under %(limit):")
for (i in 1...limit) {
    var pd = Int.properDivisors(i).skip(1)
    if (pd.count > 1 && Nums.prod(pd) == i) {
        var pds = pd.map { |d| Fmt.d(3, d) }.join(" x ")
        Fmt.print("$3d  = $s", i, pds)
    }
}
Output:
Special numbers under 500:
  6  =   2 x   3
  8  =   2 x   4
 10  =   2 x   5
 14  =   2 x   7
 15  =   3 x   5
 21  =   3 x   7
 22  =   2 x  11
 26  =   2 x  13
 27  =   3 x   9
 33  =   3 x  11
 34  =   2 x  17
 35  =   5 x   7
 38  =   2 x  19
 39  =   3 x  13
 46  =   2 x  23
 51  =   3 x  17
 55  =   5 x  11
 57  =   3 x  19
 58  =   2 x  29
 62  =   2 x  31
 65  =   5 x  13
 69  =   3 x  23
 74  =   2 x  37
 77  =   7 x  11
 82  =   2 x  41
 85  =   5 x  17
 86  =   2 x  43
 87  =   3 x  29
 91  =   7 x  13
 93  =   3 x  31
 94  =   2 x  47
 95  =   5 x  19
106  =   2 x  53
111  =   3 x  37
115  =   5 x  23
118  =   2 x  59
119  =   7 x  17
122  =   2 x  61
123  =   3 x  41
125  =   5 x  25
129  =   3 x  43
133  =   7 x  19
134  =   2 x  67
141  =   3 x  47
142  =   2 x  71
143  =  11 x  13
145  =   5 x  29
146  =   2 x  73
155  =   5 x  31
158  =   2 x  79
159  =   3 x  53
161  =   7 x  23
166  =   2 x  83
177  =   3 x  59
178  =   2 x  89
183  =   3 x  61
185  =   5 x  37
187  =  11 x  17
194  =   2 x  97
201  =   3 x  67
202  =   2 x 101
203  =   7 x  29
205  =   5 x  41
206  =   2 x 103
209  =  11 x  19
213  =   3 x  71
214  =   2 x 107
215  =   5 x  43
217  =   7 x  31
218  =   2 x 109
219  =   3 x  73
221  =  13 x  17
226  =   2 x 113
235  =   5 x  47
237  =   3 x  79
247  =  13 x  19
249  =   3 x  83
253  =  11 x  23
254  =   2 x 127
259  =   7 x  37
262  =   2 x 131
265  =   5 x  53
267  =   3 x  89
274  =   2 x 137
278  =   2 x 139
287  =   7 x  41
291  =   3 x  97
295  =   5 x  59
298  =   2 x 149
299  =  13 x  23
301  =   7 x  43
302  =   2 x 151
303  =   3 x 101
305  =   5 x  61
309  =   3 x 103
314  =   2 x 157
319  =  11 x  29
321  =   3 x 107
323  =  17 x  19
326  =   2 x 163
327  =   3 x 109
329  =   7 x  47
334  =   2 x 167
335  =   5 x  67
339  =   3 x 113
341  =  11 x  31
343  =   7 x  49
346  =   2 x 173
355  =   5 x  71
358  =   2 x 179
362  =   2 x 181
365  =   5 x  73
371  =   7 x  53
377  =  13 x  29
381  =   3 x 127
382  =   2 x 191
386  =   2 x 193
391  =  17 x  23
393  =   3 x 131
394  =   2 x 197
395  =   5 x  79
398  =   2 x 199
403  =  13 x  31
407  =  11 x  37
411  =   3 x 137
413  =   7 x  59
415  =   5 x  83
417  =   3 x 139
422  =   2 x 211
427  =   7 x  61
437  =  19 x  23
445  =   5 x  89
446  =   2 x 223
447  =   3 x 149
451  =  11 x  41
453  =   3 x 151
454  =   2 x 227
458  =   2 x 229
466  =   2 x 233
469  =   7 x  67
471  =   3 x 157
473  =  11 x  43
478  =   2 x 239
481  =  13 x  37
482  =   2 x 241
485  =   5 x  97
489  =   3 x 163
493  =  17 x  29
497  =   7 x  71