我们知道可以使用以下方法生成3以上的所有素数:
6 * k + 1
6 * k - 1
Run Code Online (Sandbox Code Playgroud)
但是,我们从上面的公式生成的所有数字都不是素数.
For Example:
6 * 6 - 1 = 35 which is clearly divisible by 5.
Run Code Online (Sandbox Code Playgroud)
为了消除这些条件,我使用Sieve方法并删除了数字,这些数字是从上面公式生成的数字的因子.
使用事实:
如果没有素数因素,那么一个数字被称为素数.
生成低于1000的素数.
ArrayList<Integer> primes = new ArrayList<>();
primes.add(2);//explicitly add
primes.add(3);//2 and 3
int n = 1000;
for (int i = 1; i <= (n / 6) ; i++) {
//get all the numbers which can be generated by the formula
int prod6k = 6 * i;
primes.add(prod6k - 1); …
Run Code Online (Sandbox Code Playgroud) 我有这种格式的JSON对象.
[
{
"name": "schoolname",
"line id": "0",
"time": "4-5",
"minage": "15",
"maxage": "35"
},
{
"name": "studentname1",
"line id": "1",
"class": "A"
},
{
"name": "studentname2",
"line id": "2",
"class": "B"
}
]
Run Code Online (Sandbox Code Playgroud)
我想做的事
从一组指定的标题中,从中获取并将其"line id" : "0"
设置为其他项目.
例如:
headers = ["time", "minage", "maxage"]
我从中获取这些"line id" : "0"
并将其交给其他人.
[
{
"name": "schoolname",
"line id": "0",
"time": "4-5",
"minage": "15",
"maxage": "35"
},
{
"name": "studentname1",
"line id": "1",
"class": "A",
"time": "4-5",
"minage": "15",
"maxage": …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个代码来计算给定整数的以下内容n
:
1/1 + 1/2 + 1/3 ... + 1/n
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止编写的代码:
public class RecursiveSum
{
public static double Sumto(int n)
{
if (n == 0) { return 0.0; }
else if (n > 0) { return 1/n + 1/Sumto(n - 1); }
else { throw new IllegalArgumentException("Please provide positive integers"); }
}
public static void main(String[] args)
{
System.out.println(Sumto(5));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它始终输出:
Infinity
Run Code Online (Sandbox Code Playgroud)
有什么问题,我该如何解决?
谢谢
从数学原理:
数字N可表示为2个平方的总和,当且仅当在N的素数因子分解中,形式的每个素数都
(4k+3)
出现偶数次!
我所做的是预先计算所有4k+3
数字并通过连续分割检查它的频率.
该程序是根据约束编写的:
1 < T <100
0 < N < 10 ^ 12
Run Code Online (Sandbox Code Playgroud)
import java.util.Scanner;
public class TwoSquaresOrNot {
static int max = 250000;
static long[] nums = new long[max];
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < max; ++i)
nums[i] = 4 * i + 3;
while (T-- > 0) {
long n = sc.nextLong();
System.out.println((canWrite(n) ? "Yes" : …
Run Code Online (Sandbox Code Playgroud) 我试图使用OpenPgpjs加密和签名消息.
但我不断收到此错误"错误加密邮件:私钥未解密"
这是我试过的:
var openpgp = require('openpgp');
var publicKey = [].join("\n"); //This has the complete key. Removed for representation
var privateKey = [].join("\n"); //This has the complete key. Removed for representation
var publicKeys = openpgp.key.readArmored(publicKey).keys;
var privateKeys = openpgp.key.readArmored(privateKey).keys;
encryptionOptions = {
data : 'Example Test',
publicKeys : publicKeys,
privateKeys : privateKeys
};
return openpgp.encrypt(encryptionOptions).then(function(ciphertext) {
encryptedData = ciphertext.data;
console.log(ciphertext);
return encryptedData;
});
Run Code Online (Sandbox Code Playgroud) 假设我需要继续询问用户,直到他进入double
.
我做的是我使用了一个while循环并检查是否有异常.
如果有异常,我会去询问下一个输入.
double bal = 0;
Scanner sc = new Scanner(System.in);
while (true) {
try {
System.out.println("Enter the balance");
bal = sc.nextDouble();
break;
} catch (Exception e) {
System.out.println("That isn't a number");
}
}
System.out.println("Bal is " + bal);
sc.close();
Run Code Online (Sandbox Code Playgroud)
但是如果我输入一个非double,那么它不会要求下一个输入,继续打印以无限循环结束的那两行.
Enter the balance
XYZ
That isn't a number
Enter the balance
That isn't a number
Enter the balance
That isn't a number
Enter the balance
That isn't a number
....
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
java ×4
javascript ×2
arrays ×1
double ×1
json ×1
node.js ×1
numbers ×1
openpgp.js ×1
optimization ×1
primes ×1
recursion ×1
sieve ×1