Calmo numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Algol 68)
Line 69: Line 69:
<pre>
<pre>
165 273 385 399 561 595 665 715 957
165 273 385 399 561 595 665 715 957
</pre>

=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "works..." + nl
numCalmo = 0
limit = 1000
for n = 1 to limit
Calmo = []
for m = 2 to n/2
if n % m = 0
add(Calmo,m)
ok
next
flag = 1
lenCalmo = len(Calmo)
if (lenCalmo > 5) and (lenCalmo % 3 = 0)
for p = 1 to lenCalmo - 2 step 3
sum = Calmo[p] + Calmo[p+1] + Calmo[p+2]
if not isPrime(sum)
flag = 0
exit
ok
next
if flag = 1
numCalmo++
see "n(" + numCalmo + ") = " + n + nl
see "divisors = ["
for p = 1 to lenCalmo - 2 step 3
sumCalmo = Calmo[p] + Calmo[p+1] + Calmo[p+2]
if not isPrime(sumCalmo)
exit
else
if p = 1
see "" + Calmo[p] + " " + Calmo[p+1] + " " + Calmo[p+2]
else
see " " + Calmo[p] + " " + Calmo[p+1] + " " + Calmo[p+2]
ok
ok
next
see "]" + nl
for p = 1 to lenCalmo - 2 step 3
sumCalmo = Calmo[p] + Calmo[p+1] + Calmo[p+2]
if isPrime(sumCalmo)
see "" + Calmo[p] + " + " + Calmo[p+1] + " + " + Calmo[p+2] + " = " + sumCalmo + " is prime" + nl
ok
next
see nl
ok
ok
next
see "Found " + numCalmo + " Calmo numbers" + nl
see "done..." + nl

func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1
</syntaxhighlight>
{{out}}
<pre>
works...
n(1) = 165
divisors = [3 5 11 15 33 55]
3 + 5 + 11 = 19 is prime
15 + 33 + 55 = 103 is prime

n(2) = 273
divisors = [3 7 13 21 39 91]
3 + 7 + 13 = 23 is prime
21 + 39 + 91 = 151 is prime

n(3) = 385
divisors = [5 7 11 35 55 77]
5 + 7 + 11 = 23 is prime
35 + 55 + 77 = 167 is prime

n(4) = 399
divisors = [3 7 19 21 57 133]
3 + 7 + 19 = 29 is prime
21 + 57 + 133 = 211 is prime

n(5) = 561
divisors = [3 11 17 33 51 187]
3 + 11 + 17 = 31 is prime
33 + 51 + 187 = 271 is prime

n(6) = 595
divisors = [5 7 17 35 85 119]
5 + 7 + 17 = 29 is prime
35 + 85 + 119 = 239 is prime

n(7) = 665
divisors = [5 7 19 35 95 133]
5 + 7 + 19 = 31 is prime
35 + 95 + 133 = 263 is prime

n(8) = 715
divisors = [5 11 13 55 65 143]
5 + 11 + 13 = 29 is prime
55 + 65 + 143 = 263 is prime

n(9) = 957
divisors = [3 11 29 33 87 319]
3 + 11 + 29 = 43 is prime
33 + 87 + 319 = 439 is prime

Found 9 Calmo numbers
done...
</pre>
</pre>

Revision as of 12:52, 19 March 2023

Definition Let n be a natural number. Let the real divisors of n be: d(1),d(2),d(3),...,d(k), where k is divisible by 3. Add the first three divisors, then the next three, and so on. If the partial sums are prime numbers, then n is called a Calmo number. Example n = 165 divisors = [3 5 11 15 33 55] 3 + 5 + 11 = 19 is prime number 15 + 33 + 55 = 103 is prime number Task let's find Calmo numbers under 1000.


ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

Note, the source of primes.incl.a68 is on Rosetta Code (see above link).
Assuming the task author intended that 1 and n should be excluded when considering the divisors of n, the output confirms the task author's original sample which they deleted for some reason.

BEGIN # find some "Calmo" numbers: numbers n such that they have 3k divisors  #
      # (other than 1 and n) for some k > 0 and the sum of their divisors     #
      # taken three at a time is a prime                                      #
    PR read "primes.incl.a68" PR      # include prime utilities               #
    INT max number = 1 000;           # largest number we will consider       #
    # construct a sieve of (hopefully) enough primes - as we are going to sum #
    # the divisors in groups of three, it should be (more than) large enough  #
    []BOOL prime = PRIMESIEVE ( max number * 3 );
    # construct tables of the divisor counts and divisor sums and check for   #
    # the numbers as we do it                                                 #
    # as we are ignoring 1 and n, the initial counts and sums will be 0       #
    # but we should ignore primes                                             #
    [ 1 : max number ]INT dsum;
    [ 1 : max number ]INT dcount;
    FOR i TO UPB dcount DO
        dsum[ i ] := dcount[ i ] := IF prime[ i ] THEN -1 ELSE 0 FI
    OD;
    FOR i FROM 2 TO UPB dsum
        DO FOR j FROM i + i BY i TO UPB dsum DO
            # have another proper divisor                                     #
            IF dsum[ j ] >= 0 THEN
                # this number is still a candidate                            #
                dsum[   j ] +:= i;
                dcount[ j ] +:= 1;
                IF dcount[ j ] = 3 THEN
                    # the divisor count is currently 3                        #
                    dsum[ j ] := dcount[ j ] := 
                        IF NOT prime[ dsum[ j ] ] THEN
                            # divisor sum isn't prime, ignore it in future    #
                            -1
                        ELSE
                            # divisor count is prime, reset the sum and count #
                            0
                        FI
                FI
            FI
        OD
    OD;
    # show the numbers                                                        #
    FOR i FROM 2 TO UPB dcount DO
        IF dcount[ i ] = 0 THEN
            # have a number                                                   #
            print( ( " ", whole( i, 0 ) ) )
        FI
    OD;
    print( ( newline ) )
END
Output:
 165 273 385 399 561 595 665 715 957

Ring

see "works..." + nl
numCalmo = 0
limit = 1000
for n = 1 to limit
    Calmo = []
    for m = 2 to n/2
        if n % m = 0
           add(Calmo,m)
        ok
    next
    flag = 1
    lenCalmo = len(Calmo)
    if (lenCalmo > 5) and (lenCalmo % 3 = 0)
        for p = 1 to lenCalmo - 2 step 3
            sum = Calmo[p] + Calmo[p+1] + Calmo[p+2]
            if not isPrime(sum)
               flag = 0
               exit
            ok
        next
        if flag = 1
           numCalmo++
           see "n(" + numCalmo + ") = " + n + nl
           see "divisors = ["
           for p = 1 to lenCalmo - 2 step 3
               sumCalmo = Calmo[p] + Calmo[p+1] + Calmo[p+2]
               if not isPrime(sumCalmo)
                  exit
               else
                  if p = 1
                     see "" + Calmo[p] + " " + Calmo[p+1] + " " + Calmo[p+2]
                  else
                     see " " + Calmo[p] + " " + Calmo[p+1] + " " + Calmo[p+2]
                  ok
               ok 
            next
            see "]" + nl
            for p = 1 to lenCalmo - 2 step 3
                sumCalmo = Calmo[p] + Calmo[p+1] + Calmo[p+2]
                if isPrime(sumCalmo)
                   see "" + Calmo[p] + " + " + Calmo[p+1] + " + " + Calmo[p+2] + " = " + sumCalmo + " is prime" + nl
                ok
            next
            see nl
        ok
     ok
next
see "Found " + numCalmo + " Calmo numbers" + nl
see "done..." + nl

func isPrime num
     if (num <= 1) return 0 ok
     if (num % 2 = 0 and num != 2) return 0 ok
     for i = 3 to floor(num / 2) -1 step 2
         if (num % i = 0) return 0 ok
     next
     return 1
Output:
works...
n(1) = 165
divisors = [3 5 11 15 33 55]
3 + 5 + 11 = 19 is prime
15 + 33 + 55 = 103 is prime

n(2) = 273
divisors = [3 7 13 21 39 91]
3 + 7 + 13 = 23 is prime
21 + 39 + 91 = 151 is prime

n(3) = 385
divisors = [5 7 11 35 55 77]
5 + 7 + 11 = 23 is prime
35 + 55 + 77 = 167 is prime

n(4) = 399
divisors = [3 7 19 21 57 133]
3 + 7 + 19 = 29 is prime
21 + 57 + 133 = 211 is prime

n(5) = 561
divisors = [3 11 17 33 51 187]
3 + 11 + 17 = 31 is prime
33 + 51 + 187 = 271 is prime

n(6) = 595
divisors = [5 7 17 35 85 119]
5 + 7 + 17 = 29 is prime
35 + 85 + 119 = 239 is prime

n(7) = 665
divisors = [5 7 19 35 95 133]
5 + 7 + 19 = 31 is prime
35 + 95 + 133 = 263 is prime

n(8) = 715
divisors = [5 11 13 55 65 143]
5 + 11 + 13 = 29 is prime
55 + 65 + 143 = 263 is prime

n(9) = 957
divisors = [3 11 29 33 87 319]
3 + 11 + 29 = 43 is prime
33 + 87 + 319 = 439 is prime

Found 9 Calmo numbers
done...