primes

Calculating and printing the nth prime number

走远了吗. 提交于 2019-12-17 01:40:08
问题 I am trying to calculate prime numbers, which I've already done. But I want to calculate and print ONLY the nth prime number (User input), while calculating the rest (They won't be printed) only the nth prime number will be printed. Here's what I've written so far: import java.util.Scanner; /** * Calculates the nth prime number * @author {Zyst} */ public class Prime { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n, i = 2, x = 2; System.out.printf("This

What would be the fastest method to test for primality in Java?

你离开我真会死。 提交于 2019-12-17 01:40:06
问题 I am trying to find the fastest way to check whether a given number is prime or not (in Java). Below are several primality testing methods I came up with. Is there any better way than the second implementation(isPrime2)? public class Prime { public static boolean isPrime1(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } for (int i = 2; i <= Math.sqrt(n) + 1; i++) { if (n % i == 0) { return false; } } return true; } public static boolean isPrime2(int n) { if (n <= 1) {

Optimize prime number finder and cpu speed checker in R

核能气质少年 提交于 2019-12-16 18:06:10
问题 I have following code to find prime numbers in 10 seconds: prime_nums = function (){ ptm <- proc.time() p_nums = c(2) counter = 2 while (TRUE){ isPRIME = FALSE counter = counter +1 for(n in p_nums) { if(n > sqrt(counter)){ isPRIME=TRUE; break; } if(counter %% n == 0){ isPRIME = FALSE; break;} } if(isPRIME) { p_nums[length(p_nums)+1]=counter ; cat("",counter,";")} if((proc.time()[3]-ptm[3]) > 10) break; } } However, this is written with many loops which are generally not preferred in R. How

What is an efficient algorithm to find all the factors of an integer?

China☆狼群 提交于 2019-12-14 03:55:53
问题 I was writing a very simple program to examine if a number could divide another number evenly: // use the divider squared to reduce iterations for(divider = 2; (divider * divider) <= number; divider++) if(number % divider == 0) print("%d can divided by %d\n", number, divider); Now I was curious if the task could be done by finding the square root of number and compare it to divider. However, it seems that sqrt() isn't really able to boost the efficiency. How was sqrt() handled in C and how

Find sum of first 1000 prime numbers in python [duplicate]

别来无恙 提交于 2019-12-14 03:37:59
问题 This question already has answers here : To find first N prime numbers in python (30 answers) Closed 4 years ago . I have written a program which counts the sum of the primes uptill 1000. The program is as follows: limit = 1000 def is_prime(n): for i in range(2, n): if n%i == 0: return False return True sum = 0 for i in range(2, int(limit+1)): if is_prime(i): sum = sum + i count += 1 print sum What changes can I make to find 1000 primes instead of upto 1000 numbers? Also, I am looking for

Goldbach’s Conjecture in prolog

Deadly 提交于 2019-12-14 03:34:29
问题 Goldbach’s Conjecture : Every positive even number greater than 2 is the sum of two prime numbers. Eg 28 (5,23 and 11,17) I want Prolog code to print below (all combinations) : ?- goldbach(28, L). Output : L = [5,23]; L = [11, 17]; I have a code which prints single combination[5,23], but not the next [11,17]. is_prime(2). is_prime(3). is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ has_factor(P,3). has_factor(N,L) :- N mod L =:= 0. has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N

Is there a fast, functional prime generator?

荒凉一梦 提交于 2019-12-14 00:22:23
问题 Suppose I've got a natural number n and I want a list (or whatever) of all primes up to n . The classic prime sieve algorithm runs in O(n log n) time and O(n) space -- it's fine for more imperative languages, but requires in-place modification to lists and random access, in a fundamental way. There's a functional version involving priority queues, which is pretty slick -- you can check it out here. This has better space complexity at about O(n / log(n)) (asymptotically better but debatable at

Optimizing prime numbers code?

◇◆丶佛笑我妖孽 提交于 2019-12-13 18:55:35
问题 I wrote this code to show the primes between 1 and 100. The only condition is to do not use functions, whole code should be inline. I would ask if I can improve (optimize) it much more? #include<iostream> using namespace std; int main() { int i=2,j=2; cout<<"Prime numbers between 1 and 100 are:"<<endl; cout<<"2"<<"\t"; while(i!=100) { for(int j=2;j<i;j++) { if(i%j==0) break; if(j==i-1) cout<<i<<"\t"; } i++; } cout<<endl; system("pause"); return 0; } 回答1: You are checking every number from 2

Finding the nth prime in the set of prime numbers.

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 18:49:00
问题 This application will receive a number "n". After receiving this number, the program has to show the nth prime in the list of primes. For example, if the user enters "3", the program is supposed to display "5", because 5 is the third prime starting at 2. I know that something is wrong with my code but I don't know where the problem is and how I can fix it. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program {

Prime number just below a number [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-13 17:14:33
问题 This question already has an answer here : Find a largest prime number less than n [closed] (1 answer) Closed 6 years ago . I want to calculate prime number just below a number. How can this be done efficiently. I used Sieve of Eratosthenes but it failed as my numbers are in range 10^20 Any other algorithm?? 回答1: The chance of a random 20-digit number being prime is approximately 1/20 (source). If you want the largest prime below x , start at x -1 and run a primality test on each number,