标签:next func 不能 comment n的阶乘 ast boolean 数学逻辑 杂志
数学离程序员有多近?
ifelse也好、for循环也罢,代码可以说就是对数学逻辑的具体实现。所以敲代码的程序员几乎就离不开数学,难易不同而已。
那数学不好就写不了代码吗???不,一样可以写代码,可以写出更多的CRUD
出来。那你不要总觉得是产品需求简单所以你的实现过程才变成了增删改查,往往也是因为你还不具备可扩展、易维护、高性能的代码实现方案落地能力,才使得你小小年纪写出了更多的CRUD
!
与一锥子买卖的小作坊相比,大厂和超级大厂更会注重数学能力。
2004年,在硅谷的交通动脉 101 公路上突然出现一块巨大的广告牌,上面是一道数学题: {e 的连续数字中最先出现的 10 位质数}
.com。
广告:这里的 e 是数学常数,自然对数的底数,无限不循环小数。这道题的意思就是,找出 e 中最先出现的 10 位质数,然后可以得出一个网址。进入这个网址会看到 Google 为你出的第二道数学题,成功解锁这步 Google 会告诉你,我们或许是”志同道合“的人
,你可以将简历发到这个邮箱,我们一起做点改变世界的事情。
计算 e 值可以通过泰勒公式推导出来:e^x≈1 + x + x^2/2! + x^3/3! +……+ x^n/n! (1) 推导计算过程还包括埃拉托色尼筛选法(the Sieve of Eratosthenes)
、线性筛选法
的使用。感兴趣的小伙伴可以用代码实现下。
@Test
public void test_Fibonacci() {
int month = 15; // 15个月
long f1 = 1L, f2 = 1L;
long f;
for (int i = 3; i < month; i++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.println("第" + i + "个月的兔子对数: " + f2);
}
}
@Test
public void test_Prime() {
int count = 0;
for (int i = 101; i < 200; i++) {
boolean b = true;// 默认此数就素数
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
b = false; // 此数不是素数
break;
}
}
if (b) {
count++;
System.out.print(i + " ");
}
}
System.out.println("\n素数的个数:" + count);
}
@Test
public void test_narcissus() {
for (int num = 101; num < 1000; num++) {
int bbb = num / 100;
int bb = (num % 100) / 10;
int b = (num % 100) % 10;
if ((bbb * bbb * bbb + bb * bb * bb + b * b * b) == num) {
System.out.println(num);
}
}
}
@Test
public void test_ZhiYinShu() {
f(200);
}
int k = 2;
public void f(int n) {
while (k <= n) {
if (k == n) {
System.out.println(n);
break;
} else if (n > k && n % k ==
System.out.print(k + "*")
n = n / k;
f(n);
break;
} else if (n > k && n % k !=
k++;
f(n);
break;
}
}
}
@Test
public void test_YangHuiSanJiao(){
int[][] a = new int[10][10];
for (int i = 0; i < 10; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (int i = 2; i < 10; i++) {
for (int j = 1; j < i; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 2 * (10 - i) - 1; k++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
@Test
public void test_Prime() {
int a = 10, b = 24;
int m = division(a, b);
int n = a * b / m;
System.out.println("最大公约数: " + m);
System.out.println("最小公倍数: " + n);
}
public int division(int x, int y) {
int t;
if (x < y) {
t = x;
x = y;
y = t;
}
while (y != 0) {
if (x == y)
return 1;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
@Test
public void test_PerfectSquare() {
for (long l = 1L; l < 100000; l++) {
if (Math.sqrt((l + 100)) % 1 == 0) {
if (Math.sqrt((l + 268)) % 1 == 0) {
System.out.println(l + "加100是一个完全平方数,再加168又是一个完全平方数");
}
}
}
}
@Test
public void test_Sum() {
Scanner s = new Scanner(System.in);
int[][] a = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("输入的3 * 3 矩阵是:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
sum += a[i][j];
}
}
}
System.out.println("对角线和是 " + sum);
}
@Test
public void test_solution() {
System.out.println("1到1000的完数有: ");
for (int i = 1; i < 1000; i++) {
int t = 0;
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
t = t + j;
}
}
if (t == i) {
System.out.print(i + " ");
}
}
}
@Test
public void test_asum() {
long a = 2, b = 0;
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int i = 0;
long sum = 0;
while (i < n) {
b = b + a;
sum = sum + b;
a = a * 10;
++i;
}
System.out.println("input number: " + n);
System.out.println(sum);
}
@Test
public void test_AC() {
int count = 0;
for (int x = 1; x < 5; x++) {
for (int y = 1; y < 5; y++) {
for (int z = 1; z < 5; z++) {
if (x != y && y != z && x != z) {
count++;
System.out.print(x * 100 + y * 10 + z + " ");
if (count % 4 == 0) {
System.out.println();
}
}
}
}
}
System.out.println("共有" + count + "个三位数");
}
public class SmallToBig {
public static void main(String[] args) {
SmallToBig fnc = new SmallToBig();
int a, b, c;
System.out.println("Input 3 numbers:");
a = fnc.input();
b = fnc.input();
c = fnc.input();
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a > c) {
int t = a;
a = c;
c = t;
}
if (b > c) {
int t = b;
b = c;
c = t;
}
System.out.println(a + " " + b + " " + c);
}
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
public void compare(int x, int y) {// 此方法没用
if (x > y) {
int t = x;
x = y;
y = t;
}
}
}
public class Monkey {
public static void main(String[] args) {
int lastdayNum = 1;
for (int i = 2; i <= 10; i++) {
lastdayNum = (lastdayNum + 1) * 2;
}
System.out.println("猴子第一天摘了 " + lastdayNum + " 个桃子");
}
}
public class Compete {
static char[] m = { ‘a‘, ‘b‘, ‘c‘ };
static char[] n = { ‘x‘, ‘y‘, ‘z‘ };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++) {
if (m[i] == ‘a‘ && n[j] == ‘x‘) {
continue;
} else if (m[i] == ‘a‘ && n[j] == ‘y‘) {
continue;
} else if ((m[i] == ‘c‘ && n[j] == ‘x‘)
|| (m[i] == ‘c‘ && n[j] == ‘z‘)) {
continue;
} else if ((m[i] == ‘b‘ && n[j] == ‘z‘)
|| (m[i] == ‘b‘ && n[j] == ‘y‘)) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
public class FenShu {
public static void main(String[] args) {
int x = 2, y = 1, t;
double sum = 0;
DecimalFormat df = new DecimalFormat("#0.0000");
for (int i = 1; i <= 20; i++) {
sum += (double) x / y;
t = y;
y = x;
x = y + t;
System.out.println("第 " + i + " 次相加,和是 " + df.format(sum));
}
}
}
public class JieCheng {
static long sum = 0;
static long fac = 0;
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for (int i = 1; i <= 10; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
}
public class HuiWen {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("请输入一个正整数:");
long a = s.nextLong();
String ss = Long.toString(a);
char[] ch = ss.toCharArray();
boolean is = true;
int j = ch.length;
for (int i = 0; i < j / 2; i++) {
if (ch[i] != ch[j - i - 1]) {
is = false;
}
}
if (is == true) {
System.out.println("这是一个回文数");
} else {
System.out.println("这不是一个回文数");
}
}
}
public class ShunXu {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a < c) {
int t = a;
a = c;
c = t;
}
if (b < c) {
int t = b;
b = c;
c = t;
}
System.out.println("从大到小的顺序输出:");
System.out.println(a + " " + b + " " + c);
}
}
public class TiHuan {
static final int N = 8;
public static void main(String[] args) {
int[] a = new int[N];
Scanner s = new Scanner(System.in);
int index1 = 0, index2 = 0;
System.out.println("please input numbers");
for (int i = 0; i < N; i++) {
a[i] = s.nextInt();
System.out.print(a[i] + " ");
}
int max = a[0], min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
index1 = i;
}
if (a[i] < min) {
min = a[i];
index2 = i;
}
}
if (index1 != 0) {
int temp = a[0];
a[0] = a[index1];
a[index1] = temp;
}
if (index2 != a.length - 1) {
int temp = a[a.length - 1];
a[a.length - 1] = a[index2];
a[index2] = temp;
}
System.out.println("after swop:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
long startTime = System.currentTimeMillis();
int num = 10000000, saveNum = 1, countNum = 0, lastNum = 0;
int copyNum = num;
while (num != 0) {
lastNum = num % 10;
num /= 10;
if (lastNum == 0) {
// 如果是0那么正好是少了一次所以num不加1了
countNum += num * saveNum;
} else if (lastNum == 1) {
// 如果是1说明当前数内少了一次所以num不加1,而且当前1所在位置
// 有1的个数,就是去除当前1最高位,剩下位数,的个数。
countNum += num * saveNum + copyNum % saveNum + 1;
} else {
// 如果非1非0.直接用公式计算
// abcd...=(abc+1)*1+(ab+1)*10+(a+1)*100+(1)*1000...
countNum += (num + 1) * saveNum;
}
saveNum *= 10;
}
System.out.println("1的个数:" + countNum);
System.out.println("计算耗时:" + (System.currentTimeMillis() - startTime) + "毫秒");
Java 代码本身就是基于数据结构和算法对数学逻辑的具体实现,而那些隐含在代码中的数学知识如果你不会,那么压根你就会忽略掉它,也就因此看不懂源码了。
就像我问你:
所以小傅哥整理了一本,《Java 面经手册》 是一本以面试题为入口讲解 Java 核心技术的 PDF 书籍,书中内容也极力的向你证实代码是对数学逻辑的具体实现
。为什么这么说? 当你仔细阅读书籍时,会发现这里有很多数学知识,包括:扰动函数、负载因子、拉链寻址、开放寻址、斐波那契(Fibonacci)散列法还有黄金分割点的使用等等。
标签:next func 不能 comment n的阶乘 ast boolean 数学逻辑 杂志
原文地址:https://www.cnblogs.com/shenminle/p/14780439.html