质数概念 质数,又称素数,指在一个大于1的自然数中,除了1和此整数自身外,无法被其他自然数整除的数(也可定义为只有1和本身两个因数的数).最小的素数是2,也是素数中唯一的偶数:其他素数都是奇数.质数有无限多个,所以不存在最大的质数. 目前总结大概有3中计算方式求解,具体如下 1. 粗鲁暴力定义求解法 public class Prime { // find the prime between 1 to 1000; public static void main(String[] args) {…
import java.util.Scanner; /** * Created by Admin on 2017/3/25. */ public class test01 { public static boolean IsPrime(int n){ if (n<2) return false; if (n==2) return true; if (n>2) for (int i=2;i<=(int)Math.sqrt(n);i++){ if (n%i==0) return false;…
题目描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 迭代解法 /** Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } */ class Solution { public ListNode…
题目描述: 源码: 运用Java大数求解. import java.math.BigInteger; import java.util.*; public class Main { //主函数 public static void main(String[] args) { int n, index; BigInteger f1, f2, fn; Scanner cin = new Scanner(System.in); n = cin.nextInt(); for(int i = 0; i <…
题目链接:Twice Equation 比赛链接:ICPC Asia Nanning 2017 Description For given \(L\), find the smallest \(n\) no smaller than \(L\) for which there exists an positive integer \(m\) for which \(2m(m + 1) = n(n + 1)\). Input This problem contains multiple test…
面试中,遇到一个题目:求解第N个素数. import java.util.Scanner; public class GetPrimeNumber { public static int NthPrime(int n){ int i = 2, j = 1; while (true) { j = j + 1; if (j > i / j) { n--; if (n == 0) break; j = 1; } if (i % j == 0) { i++; j = 1; } } return i; }…