chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
PROGS = $(patsubst %.c,%,$(SRCS))
|
||||
|
||||
all: $(PROGS)
|
||||
|
||||
%: %.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f $(PROGS)
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Most of the C compilers support a third parameter to main which
|
||||
// store all envorinment variables
|
||||
int main(int argc, char *argv[], char * envp[])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; envp[i] != NULL; i++)
|
||||
printf("\n%s", envp[i]);
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
link("env", "env3");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int ret = openat(AT_FDCWD, "/dev/tty", 0x88102, 0);
|
||||
printf("return value is %d and errno is %d\n", ret, errno);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int status;
|
||||
|
||||
pid_t p = getpid();
|
||||
// waitpid takes a children's pid, not the current process one
|
||||
// if the pid is not a children of the current process, it returns -ECHILD
|
||||
pid_t res = waitpid(1001, &status, WNOHANG);
|
||||
|
||||
printf("res is %d, p is %d and errno is %d\n", res, p, errno);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env luajit
|
||||
cfizz,cbuzz=0,0
|
||||
for i=1,20 do
|
||||
cfizz=cfizz+1
|
||||
cbuzz=cbuzz+1
|
||||
io.write(i .. ": ")
|
||||
if cfizz~=3 and cbuzz~=5 then
|
||||
io.write(i)
|
||||
else
|
||||
if cfizz==3 then
|
||||
io.write("Fizz")
|
||||
cfizz=0
|
||||
end
|
||||
if cbuzz==5 then
|
||||
io.write("Buzz")
|
||||
cbuzz=0
|
||||
end
|
||||
end
|
||||
io.write("\n")
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env luajit
|
||||
fruits = {"banana","orange","apple","grapes"}
|
||||
|
||||
for k,v in ipairs(fruits) do
|
||||
print(k,v)
|
||||
end
|
||||
|
||||
table.sort(fruits)
|
||||
print("sorted table")
|
||||
|
||||
for k,v in ipairs(fruits) do
|
||||
print(k,v)
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env luajit
|
||||
A = { ["John"] = true, ["Bob"] = true, ["Mary"] = true, ["Elena"] = true }
|
||||
B = { ["Jim"] = true, ["Mary"] = true, ["John"] = true, ["Bob"] = true }
|
||||
|
||||
A_B = {}
|
||||
for a in pairs(A) do
|
||||
if not B[a] then A_B[a] = true end
|
||||
end
|
||||
|
||||
B_A = {}
|
||||
for b in pairs(B) do
|
||||
if not A[b] then B_A[b] = true end
|
||||
end
|
||||
|
||||
for a_b in pairs(A_B) do
|
||||
print( a_b )
|
||||
end
|
||||
for b_a in pairs(B_A) do
|
||||
print( b_a )
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
console.log("process.uptime = ", global.process.uptime());
|
||||
console.log("process.title = ", global.process.title);
|
||||
console.log("process version = ", global.process.version);
|
||||
console.log("process.platform = ", global.process.platform);
|
||||
console.log("process.cwd = ", global.process.cwd());
|
||||
console.log("process.uptime = ", global.process.uptime());
|
||||
@@ -0,0 +1,161 @@
|
||||
const PI = Math.PI;
|
||||
const SOLAR_MASS = 4 * PI * PI;
|
||||
const DAYS_PER_YEAR = 365.24;
|
||||
|
||||
function Body(x, y, z, vx, vy, vz, mass) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.vx = vx;
|
||||
this.vy = vy;
|
||||
this.vz = vz;
|
||||
this.mass = mass;
|
||||
}
|
||||
|
||||
function Jupiter() {
|
||||
return new Body(
|
||||
4.84143144246472090e+00,
|
||||
-1.16032004402742839e+00,
|
||||
-1.03622044471123109e-01,
|
||||
1.66007664274403694e-03 * DAYS_PER_YEAR,
|
||||
7.69901118419740425e-03 * DAYS_PER_YEAR,
|
||||
-6.90460016972063023e-05 * DAYS_PER_YEAR,
|
||||
9.54791938424326609e-04 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Saturn() {
|
||||
return new Body(
|
||||
8.34336671824457987e+00,
|
||||
4.12479856412430479e+00,
|
||||
-4.03523417114321381e-01,
|
||||
-2.76742510726862411e-03 * DAYS_PER_YEAR,
|
||||
4.99852801234917238e-03 * DAYS_PER_YEAR,
|
||||
2.30417297573763929e-05 * DAYS_PER_YEAR,
|
||||
2.85885980666130812e-04 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Uranus() {
|
||||
return new Body(
|
||||
1.28943695621391310e+01,
|
||||
-1.51111514016986312e+01,
|
||||
-2.23307578892655734e-01,
|
||||
2.96460137564761618e-03 * DAYS_PER_YEAR,
|
||||
2.37847173959480950e-03 * DAYS_PER_YEAR,
|
||||
-2.96589568540237556e-05 * DAYS_PER_YEAR,
|
||||
4.36624404335156298e-05 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Neptune() {
|
||||
return new Body(
|
||||
1.53796971148509165e+01,
|
||||
-2.59193146099879641e+01,
|
||||
1.79258772950371181e-01,
|
||||
2.68067772490389322e-03 * DAYS_PER_YEAR,
|
||||
1.62824170038242295e-03 * DAYS_PER_YEAR,
|
||||
-9.51592254519715870e-05 * DAYS_PER_YEAR,
|
||||
5.15138902046611451e-05 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Sun() {
|
||||
return new Body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, SOLAR_MASS);
|
||||
}
|
||||
|
||||
const bodies = Array(Sun(), Jupiter(), Saturn(), Uranus(), Neptune());
|
||||
|
||||
function offsetMomentum() {
|
||||
let px = 0;
|
||||
let py = 0;
|
||||
let pz = 0;
|
||||
const size = bodies.length;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const body = bodies[i];
|
||||
const mass = body.mass;
|
||||
px += body.vx * mass;
|
||||
py += body.vy * mass;
|
||||
pz += body.vz * mass;
|
||||
}
|
||||
|
||||
const body = bodies[0];
|
||||
body.vx = -px / SOLAR_MASS;
|
||||
body.vy = -py / SOLAR_MASS;
|
||||
body.vz = -pz / SOLAR_MASS;
|
||||
}
|
||||
|
||||
function advance(dt) {
|
||||
const size = bodies.length;
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
const bodyi = bodies[i];
|
||||
let vxi = bodyi.vx;
|
||||
let vyi = bodyi.vy;
|
||||
let vzi = bodyi.vz;
|
||||
for (let j = i + 1; j < size; j++) {
|
||||
const bodyj = bodies[j];
|
||||
const dx = bodyi.x - bodyj.x;
|
||||
const dy = bodyi.y - bodyj.y;
|
||||
const dz = bodyi.z - bodyj.z;
|
||||
|
||||
const d2 = dx * dx + dy * dy + dz * dz;
|
||||
const mag = dt / (d2 * Math.sqrt(d2));
|
||||
|
||||
const massj = bodyj.mass;
|
||||
vxi -= dx * massj * mag;
|
||||
vyi -= dy * massj * mag;
|
||||
vzi -= dz * massj * mag;
|
||||
|
||||
const massi = bodyi.mass;
|
||||
bodyj.vx += dx * massi * mag;
|
||||
bodyj.vy += dy * massi * mag;
|
||||
bodyj.vz += dz * massi * mag;
|
||||
}
|
||||
bodyi.vx = vxi;
|
||||
bodyi.vy = vyi;
|
||||
bodyi.vz = vzi;
|
||||
}
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
const body = bodies[i];
|
||||
body.x += dt * body.vx;
|
||||
body.y += dt * body.vy;
|
||||
body.z += dt * body.vz;
|
||||
}
|
||||
}
|
||||
|
||||
function energy() {
|
||||
let e = 0;
|
||||
const size = bodies.length;
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
const bodyi = bodies[i];
|
||||
|
||||
e += 0.5 * bodyi.mass * ( bodyi.vx * bodyi.vx + bodyi.vy * bodyi.vy + bodyi.vz * bodyi.vz );
|
||||
|
||||
for (let j = i + 1; j < size; j++) {
|
||||
const bodyj = bodies[j];
|
||||
const dx = bodyi.x - bodyj.x;
|
||||
const dy = bodyi.y - bodyj.y;
|
||||
const dz = bodyi.z - bodyj.z;
|
||||
|
||||
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
e -= (bodyi.mass * bodyj.mass) / distance;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
const n = +50000000;
|
||||
|
||||
offsetMomentum();
|
||||
|
||||
console.log(energy().toFixed(9));
|
||||
const start = Date.now();
|
||||
for (let i = 0; i < n; i++) {
|
||||
advance(0.01);
|
||||
}
|
||||
const end = Date.now();
|
||||
console.log(energy().toFixed(9));
|
||||
console.log("elapsed:",end-start);
|
||||
@@ -0,0 +1,31 @@
|
||||
(function () {
|
||||
|
||||
function isPrime(p) {
|
||||
const upper = Math.sqrt(p);
|
||||
for(let i = 2; i <= upper; i++) {
|
||||
if (p % i === 0 ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Return n-th prime
|
||||
function prime(n) {
|
||||
if (n < 1) {
|
||||
throw Error("n too small: " + n);
|
||||
}
|
||||
let count = 0;
|
||||
let result = 1;
|
||||
while(count < n) {
|
||||
result++;
|
||||
if (isPrime(result)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
console.log("your prime is ", prime(100000));
|
||||
|
||||
}());
|
||||
@@ -0,0 +1,16 @@
|
||||
(function (){
|
||||
let bytes = new Uint8Array([
|
||||
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
||||
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01,
|
||||
0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
|
||||
0x03, 0x73, 0x75, 0x6d, 0x00, 0x00, 0x0a, 0x0a,
|
||||
0x01, 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a,
|
||||
0x0f, 0x0b
|
||||
]);
|
||||
|
||||
console.log(bytes);
|
||||
let mod = new WebAssembly.Module(bytes);
|
||||
let instance = new WebAssembly.Instance(mod, {});
|
||||
console.log(instance.exports);
|
||||
return instance.exports.sum(2020, 1);
|
||||
}());
|
||||
@@ -0,0 +1,9 @@
|
||||
def factorial():
|
||||
f, n = 1, 1
|
||||
while True: # First iteration:
|
||||
yield f # yield 1 to start with and then
|
||||
f, n = f * n, n+1 # f will now be 1, and n will be 2, ...
|
||||
|
||||
for index, factorial_number in zip(range(51), factorial()):
|
||||
print('{i:3}!= {f:65}'.format(i=index, f=factorial_number))
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
def fib():
|
||||
a, b = 0, 1
|
||||
while True: # First iteration:
|
||||
yield a # yield 0 to start with and then
|
||||
a, b = b, a + b # a will now be 1, and b will also be 1, (0 + 1)
|
||||
|
||||
for index, fibonacci_number in zip(range(100), fib()):
|
||||
print('{i:3}: {f:3}'.format(i=index, f=fibonacci_number))
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from decimal import Decimal, getcontext
|
||||
getcontext().prec=60
|
||||
summation = 0
|
||||
for k in range(50):
|
||||
summation = summation + 1/Decimal(16)**k * (
|
||||
Decimal(4)/(8*k+1)
|
||||
- Decimal(2)/(8*k+4)
|
||||
- Decimal(1)/(8*k+5)
|
||||
- Decimal(1)/(8*k+6)
|
||||
)
|
||||
print(summation)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
=begin
|
||||
# The famous Hello World
|
||||
# Program is trivial in
|
||||
# Ruby. Superfluous:
|
||||
#
|
||||
# * A "main" method
|
||||
# * Newline
|
||||
# * Semicolons
|
||||
#
|
||||
# Here is the Code:
|
||||
=end
|
||||
|
||||
puts "Hello World!"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Output "I love Ruby"
|
||||
say = "I love Ruby"; puts say
|
||||
|
||||
# Output "I *LOVE* RUBY"
|
||||
say['love'] = "*love*"; puts say.upcase
|
||||
|
||||
# Output "I *love* Ruby", 5 times
|
||||
5.times { puts say }
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
puts(2 ** 1)
|
||||
puts(2 ** 2)
|
||||
puts(2 ** 3)
|
||||
puts(2 ** 10)
|
||||
puts(2 ** 100)
|
||||
puts(2 ** 1000)
|
||||
Reference in New Issue
Block a user