做者:小傅哥
博客:https://bugstack.cnhtml
沉澱、分享、成長,讓本身和他人都能有所收穫!😄
數學離程序員有多近?
java
ifelse也好、for循環也罷,代碼能夠說就是對數學邏輯的具體實現。因此敲代碼的程序員幾乎就離不開數學,難易不一樣而已。git
那數學很差就寫不了代碼嗎😳?不,同樣能夠寫代碼,能夠寫出更多的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)散列法還有黃金分割點的使用等等。
Java 面經手冊,資料下載:https://codechina.csdn.net/MiddlewareDesign/doc/-/issues/8
知識的路上是發現知識的快樂,還學會知識的成就感,不斷的促使你前行
。