Skip to content

Instantly share code, notes, and snippets.

View redraiment's full-sized avatar
🏠
Working from home

Zhang, Zepeng (redraiment) redraiment

🏠
Working from home
View GitHub Profile
@redraiment
redraiment / count-primes.c
Created October 31, 2022 04:47
使用容斥原理计素数个数
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define BOUND 2000000000
int primes_count = 0;
int primes[5000] = {0};
char is_composites[45000] = {0}; // > sqrt(2e9)
@redraiment
redraiment / brainfuck.c
Created August 22, 2022 09:59
Brainfuck interpreter in pure ANSI C
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define TOKENS "><+-.,[]"
#define CODE_SEGMENT_SIZE 30000
#define STACK_SEGMENT_SIZE 1000
#define DATA_SEGMENT_SIZE 30000
#define ERROR_CODE_MISSING_FILE 1
@redraiment
redraiment / llvm-sum.c
Created August 19, 2022 05:34
A compiler sample with LLVM C API: it create an executable file, which accept two integer from stdin, and output the result of sum.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include <llvm-c/TargetMachine.h>
#define FALSE 0
#define TRUE 1
@redraiment
redraiment / hello-world.ll
Last active August 18, 2022 17:05
Hello world of Brainfuck in LLVM IR
; Translate "Hello world of brainfuck" to LLVM IR manually.
; ++++++++++
; [>+++++++>++++++++++>+++>+<<<<-]
; >++.>+.+++++++..+++.>++.<<+++++++++++++++.
; >.+++.------.--------.>+.>.
; ModuleID = 'helloworld.bf'
source_filename = "helloworld.bf"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@redraiment
redraiment / sphere.js
Last active November 14, 2021 15:04
将n个点均匀地分布在球面上。算法利用正多面体(此处选用正八面体)剖分球面。
// 球面坐标系统
var Spherical = function(radial, polar, azimuthal) {
var self = this;
self.radial = radial || 1
self.polar = polar || 0
self.azimuthal = azimuthal || 0
};
// 转换成直角坐标系统
@redraiment
redraiment / Y Combinator 简介
Last active December 23, 2020 07:44
Y Combinator (Fixed-point Combinator) 不动点组合子
Y组合子是Lambda演算的一部分,也是函数式编程的理论基础。
它是一种方法/技巧,在没有赋值语句的前提下定义递归的匿名函数。
即仅仅通过Lambda表达式这个最基本的“原子”实现循环/迭代。
颇有道生一、一生二、二生三、三生万物的感觉。
虽然Y组合子在理论上很优美,但在实际开发中并不会真的用到。
想要了解Y组合子是什么,请参见维基百科:http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator
或者知乎上的回答:http://www.zhihu.com/question/20115649
@redraiment
redraiment / daughter.prolog
Created April 9, 2020 12:06
通过人物实体、性别事实、父母关系来推算女儿关系。
% 知识库
%% 性别的实体
female(ann).
female(eve).
%% 父母的实体
parent(ann, mary).
parent(tom, eve).
parent(tom, mary).
@redraiment
redraiment / elisp.fish
Last active February 4, 2020 06:23
zsh的配置:elisp
#!/usr/bin/env fish
function perror --description '显示信息到错误输出'
if test (count $argv) -gt 0
printf -- $argv >&2
end
echo >&2
end
function raise --description '抛出异常消息并退出'
@redraiment
redraiment / DefaultKeyBinding.dict
Last active February 4, 2020 02:20
~/Library/KeyBindings/DeafultKeyBinding.dict 解决OSX Option 快捷键输出奇葩字符的问题
/*
* Ctrl(C): ^
* Option(M): ~
*/
{
"^/" = "undo:";
"^a" = "moveToBeginningOfLine:";
"^e" = "moveToEndOfLine:";
"^g" = "_cancelKey:";
"^t" = "transpose:";
@redraiment
redraiment / .zsh配置文件
Last active October 23, 2019 01:36
Z Shell 配置
ZSH在交互方面的确比Bash略胜一筹,但习惯了Bash下大小写无关补全等功能,需要对zsh的默认配置做一些调整。