沉淀、分享、成長(zhǎng),讓自己和他人都能有所收獲!😄
數(shù)學(xué)離程序員有多近?
ifelse也好、for循環(huán)也罷,代碼可以說就是對(duì)數(shù)學(xué)邏輯的具體實(shí)現(xiàn)。所以敲代碼的程序員幾乎就離不開數(shù)學(xué),難易不同而已。
那數(shù)學(xué)不好就寫不了代碼嗎😳?不,一樣可以寫代碼,可以寫出更多的CRUD
出來(lái)。那你不要總覺得是產(chǎn)品需求簡(jiǎn)單所以你的實(shí)現(xiàn)過程才變成了增刪改查,往往也是因?yàn)槟氵€不具備可擴(kuò)展、易維護(hù)、高性能的代碼實(shí)現(xiàn)方案落地能力,才使得你小小年紀(jì)寫出了更多的CRUD
!
與一錐子買賣的小作坊相比,大廠和超級(jí)大廠更會(huì)注重?cái)?shù)學(xué)能力。
2004年,在硅谷的交通動(dòng)脈 101 公路上突然出現(xiàn)一塊巨大的廣告牌,上面是一道數(shù)學(xué)題:{e 的連續(xù)數(shù)字中最先出現(xiàn)的 10 位質(zhì)數(shù)}
.com。
廣告:這里的 e 是數(shù)學(xué)常數(shù),自然對(duì)數(shù)的底數(shù),無(wú)限不循環(huán)小數(shù)。這道題的意思就是,找出 e 中最先出現(xiàn)的 10 位質(zhì)數(shù),然后可以得出一個(gè)網(wǎng)址。進(jìn)入這個(gè)網(wǎng)址會(huì)看到 Google 為你出的第二道數(shù)學(xué)題,成功解鎖這步 Google 會(huì)告訴你,我們或許是”志同道合“的人
,你可以將簡(jiǎn)歷發(fā)到這個(gè)郵箱,我們一起做點(diǎn)改變世界的事情。
計(jì)算 e 值可以通過泰勒公式推導(dǎo)出來(lái):e^x≈1 + x + x^2/2! + x^3/3! +……+ x^n/n! (1) 推導(dǎo)計(jì)算過程還包括埃拉托色尼篩選法(the Sieve of Eratosthenes)
、線性篩選法
的使用。感興趣的小伙伴可以用代碼實(shí)現(xiàn)下。
@Test
public void test_Fibonacci() {
int month = 15; // 15個(gè)月
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 + "個(gè)月的兔子對(duì)數(shù): " + f2);
}
}
@Test
public void test_Prime() {
int count = 0;
for (int i = 101; i < 200; i++) {
boolean b = true;// 默認(rèn)此數(shù)就素?cái)?shù)
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
b = false; // 此數(shù)不是素?cái)?shù)
break;
}
}
if (b) {
count++;
System.out.print(i + " ");
}
}
System.out.println("\n素?cái)?shù)的個(gè)數(shù):" + 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("最大公約數(shù): " + m);
System.out.println("最小公倍數(shù): " + 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是一個(gè)完全平方數(shù),再加168又是一個(gè)完全平方數(shù)");
}
}
}
}
@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("對(duì)角線和是 " + sum);
}
@Test
public void test_solution() {
System.out.println("1到1000的完數(shù)有: ");
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 + "個(gè)三位數(shù)");
}
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 + " 個(gè)桃子");
}
}
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("請(qǐng)輸入一個(gè)正整數(shù):");
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("這是一個(gè)回文數(shù)");
} else {
System.out.println("這不是一個(gè)回文數(shù)");
}
}
}
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說明當(dāng)前數(shù)內(nèi)少了一次所以num不加1,而且當(dāng)前1所在位置
// 有1的個(gè)數(shù),就是去除當(dāng)前1最高位,剩下位數(shù),的個(gè)數(shù)。
countNum += num * saveNum + copyNum % saveNum + 1;
} else {
// 如果非1非0.直接用公式計(jì)算
// abcd...=(abc+1)*1+(ab+1)*10+(a+1)*100+(1)*1000...
countNum += (num + 1) * saveNum;
}
saveNum *= 10;
}
System.out.println("1的個(gè)數(shù):" + countNum);
System.out.println("計(jì)算耗時(shí):" + (System.currentTimeMillis() - startTime) + "毫秒");
Java 代碼本身就是基于數(shù)據(jù)結(jié)構(gòu)和算法對(duì)數(shù)學(xué)邏輯的具體實(shí)現(xiàn),而那些隱含在代碼中的數(shù)學(xué)知識(shí)如果你不會(huì),那么壓根你就會(huì)忽略掉它,也就因此看不懂源碼了。
就像我問你:
所以小傅哥整理了一本,《Java 面經(jīng)手冊(cè)》 是一本以面試題為入口講解 Java 核心技術(shù)的 PDF 書籍,書中內(nèi)容也極力的向你證實(shí)代碼是對(duì)數(shù)學(xué)邏輯的具體實(shí)現(xiàn)
。為什么這么說? 當(dāng)你仔細(xì)閱讀書籍時(shí),會(huì)發(fā)現(xiàn)這里有很多數(shù)學(xué)知識(shí),包括:擾動(dòng)函數(shù)、負(fù)載因子、拉鏈尋址、開放尋址、斐波那契(Fibonacci)散列法還有黃金分割點(diǎn)的使用等等。
Java 面經(jīng)手冊(cè),資料下載:https://codechina.csdn.net/MiddlewareDesign/doc/-/issues/8
知識(shí)的路上是發(fā)現(xiàn)知識(shí)的快樂,還學(xué)會(huì)知識(shí)的成就感,不斷的促使你前行
。聯(lián)系客服