How to resolve the algorithm Arithmetic numbers step by step in the Arturo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic numbers step by step in the Arturo programming language

Table of Contents

Problem Statement

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. 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. 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 of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic numbers step by step in the Arturo programming language

Source code in the arturo programming language

arithmetic?: function [n][
    avg: average factors n
    zero? abs avg - to :integer avg
]

composite?: function [n]->
    not? prime? n

arithmeticsUpTo: function [lim][
    items: select.first: lim 1..=> arithmetic?
    print [(to :string lim)++"th" "arithmetic number:" last items]
    print ["Number of composite arithmetic numbers <= " last items ":" dec enumerate items => composite?]
    print ""
]

first100: select.first:100 1..=> arithmetic?

loop split.every: 10 first100 'x ->
    print map x 's -> pad to :string s 4

print "" 

arithmeticsUpTo 1000
arithmeticsUpTo 10000

; stretch goal
arithmeticsUpTo 100000
arithmeticsUpTo 1000000


  

You may also check:How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Java programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Prolog programming language
You may also check:How to resolve the algorithm Copy a string step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Small programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the C programming language