Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
|
|
@ -0,0 +1,98 @@
|
|||
import ballerina/io;
|
||||
|
||||
function divisors(int n) returns int[] {
|
||||
if n < 1 { return []; }
|
||||
int[] divisors = [];
|
||||
int[] divisors2 = [];
|
||||
int i = 1;
|
||||
int k = n % 2 == 0 ? 1 : 2;
|
||||
while i * i <= n {
|
||||
if n % i == 0 {
|
||||
divisors.push(i);
|
||||
int j = n / i;
|
||||
if j != i { divisors2.push(j); }
|
||||
}
|
||||
i += k;
|
||||
}
|
||||
if divisors2.length() > 0 {
|
||||
divisors.push(...divisors2.reverse());
|
||||
}
|
||||
return divisors;
|
||||
}
|
||||
|
||||
function findNearest(int[] a, int value) returns int {
|
||||
int count = a.length();
|
||||
int low = 0;
|
||||
int high = count - 1;
|
||||
while low <= high {
|
||||
int mid = (low + high) / 2;
|
||||
if a[mid] >= value {
|
||||
high = mid - 1;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
return low < count ? low : count;
|
||||
}
|
||||
|
||||
function commatize(int n) returns string {
|
||||
string s = n.toString();
|
||||
if n < 0 { s = s.substring(1); }
|
||||
int le = s.length();
|
||||
foreach int i in int:range(le - 3, 0, -3) {
|
||||
s = s.substring(0, i) + "," + s.substring(i);
|
||||
}
|
||||
if n >= 0 { return s; }
|
||||
return "-" + s;
|
||||
}
|
||||
|
||||
function isPrime(int n) returns boolean {
|
||||
if n < 2 { return false; }
|
||||
if n % 2 == 0 { return n == 2; }
|
||||
if n % 3 == 0 { return n == 3; }
|
||||
int d = 5;
|
||||
while d * d <= n {
|
||||
if n % d == 0 { return false; }
|
||||
d += 2;
|
||||
if n % d == 0 { return false; }
|
||||
d += 4;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function main() {
|
||||
int[] arithmetic = [1];
|
||||
int[] primes = [];
|
||||
final int lim = <int>1e6;
|
||||
int n = 3;
|
||||
while arithmetic.length() < lim {
|
||||
int[] divs = divisors(n);
|
||||
int len = divs.length();
|
||||
if len == 2 {
|
||||
primes.push(n);
|
||||
arithmetic.push(n);
|
||||
} else {
|
||||
int sum = int:sum(...divs);
|
||||
if sum % len == 0 { arithmetic.push(n); }
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
io:println("The first 100 arithmetic numbers are:");
|
||||
foreach int i in 0...99 {
|
||||
io:print(arithmetic[i].toString().padStart(4));
|
||||
if (i + 1) % 10 == 0 { io:println(); }
|
||||
}
|
||||
|
||||
foreach float f in [1e3, 1e4, 1e5, 1e6] {
|
||||
int x = <int>f;
|
||||
int last = arithmetic[x - 1];
|
||||
string xc = commatize(x);
|
||||
string lastc = commatize(last);
|
||||
io:println("\nThe ", xc, "th arithmetic number is: ", lastc);
|
||||
int pcount = findNearest(primes, last) + 1;
|
||||
if !isPrime(last) { pcount -= 1; }
|
||||
int comp = x - pcount - 1; // 1 is not composite
|
||||
string compc = commatize(comp);
|
||||
io:println(`"The count of such numbers <= ${lastc} which are composite is ${compc}.`);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,5 @@
|
|||
print "The first 100 arithmetic numbers are:"
|
||||
numfmt 0 3
|
||||
n = 1
|
||||
while aricnt <= 1e5
|
||||
divi = 1 ; divcnt = 0 ; sum = 0
|
||||
proc arith n &ari &comp .
|
||||
divi = 1
|
||||
repeat
|
||||
quot = n div divi
|
||||
until quot < divi
|
||||
|
|
@ -17,20 +14,29 @@ while aricnt <= 1e5
|
|||
.
|
||||
divi += 1
|
||||
.
|
||||
if sum mod divcnt = 0
|
||||
aricnt += 1
|
||||
if aricnt <= 100
|
||||
write n & " "
|
||||
if aricnt mod 10 = 0
|
||||
print ""
|
||||
.
|
||||
.
|
||||
if divcnt > 2
|
||||
compcnt += 1
|
||||
.
|
||||
if aricnt = 1e3 or aricnt = 1e4 or aricnt = 1e5
|
||||
ari = if sum mod divcnt = 0
|
||||
comp = if divcnt > 2
|
||||
.
|
||||
print "The first 100 arithmetic numbers are:"
|
||||
n = 1
|
||||
while cnt < 100
|
||||
arith n ari comp
|
||||
if ari = 1
|
||||
write n & " "
|
||||
cnt += 1
|
||||
compcnt += comp
|
||||
.
|
||||
n += 1
|
||||
.
|
||||
print ""
|
||||
while cnt < 1e5
|
||||
arith n ari comp
|
||||
if ari = 1
|
||||
cnt += 1
|
||||
compcnt += comp
|
||||
if cnt = 1e3 or cnt = 1e4 or cnt = 1e5
|
||||
print ""
|
||||
print aricnt & "th arithmetic number: " & n
|
||||
print cnt & "th arithmetic number: " & n
|
||||
print "Composite arithmetic numbers: " & compcnt
|
||||
.
|
||||
.
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
-- 8 May 2025
|
||||
include Settings
|
||||
|
||||
say version; say 'Arithmetic numbers'; say
|
||||
say 'ARITHMETIC NUMBERS'
|
||||
say version
|
||||
say
|
||||
numeric digits 9
|
||||
divi. = 0; a = 0; c = 0
|
||||
do i = 1
|
||||
/* Is the number arithmetic? */
|
||||
a = 0; c = 0
|
||||
do i = 1 to 1e6
|
||||
if Arithmetic(i) then do
|
||||
a = a+1
|
||||
/* Is the number composite? */
|
||||
if divi.0 > 2 then
|
||||
if Composite(i) then
|
||||
c = c+1
|
||||
/* Output control */
|
||||
if a <= 100 then do
|
||||
if a = 1 then
|
||||
say 'First 100 arithmetic numbers are'
|
||||
|
|
@ -25,14 +25,12 @@ do i = 1
|
|||
say 'Of the first' a 'numbers' c 'are composite'
|
||||
say
|
||||
end
|
||||
/* Max 1m, higher takes too long */
|
||||
if a = 1000000 then
|
||||
leave
|
||||
end
|
||||
end
|
||||
say Format(Time('e'),,3) 'seconds'
|
||||
exit
|
||||
return
|
||||
|
||||
include Numbers
|
||||
include Functions
|
||||
include Special
|
||||
include Abend
|
||||
|
|
|
|||
32
Task/Arithmetic-numbers/TypeScript/arithmetic-numbers.ts
Normal file
32
Task/Arithmetic-numbers/TypeScript/arithmetic-numbers.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
function divisors(n: number): number[] {
|
||||
const divs = [1, n];
|
||||
const sqr = Math.sqrt(n);
|
||||
for (let d = 2; d <= sqr; d++) {
|
||||
if (n % d == 0) {
|
||||
divs.push(d);
|
||||
if (d != sqr) divs.push(n / d);
|
||||
}
|
||||
} // We don't really need to sort them for this task but it's nice to make
|
||||
return divs.toSorted(function(a, b) {return a - b}); // functions reusable
|
||||
}
|
||||
|
||||
let count = 0;
|
||||
let val = 0;
|
||||
let composites = 0;
|
||||
const arithList: number[] =[];
|
||||
const printValues = [1000, 10000, 100000, 1000000];
|
||||
while (count < 10**6) {
|
||||
val += 1;
|
||||
const divList = divisors(val);
|
||||
const average = divList.reduce((a, b) => a + b) / divList.length;
|
||||
if (Number.isInteger(average)) {
|
||||
count += 1;
|
||||
if (divList.length > 2) composites++;
|
||||
if (count <= 100) arithList.push(val);
|
||||
if (count == 100) console.log(arithList);
|
||||
if (printValues.includes(count)) {
|
||||
console.log("The " + count + "th arithmetic number is " + val);
|
||||
console.log(composites + " of the first " + count + " are composite\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue