primes

Finding the 10001st prime number (in python)? [duplicate]

别来无恙 提交于 2019-12-12 03:24:56
问题 This question already has answers here : Fastest way to list all primes below N (34 answers) Closed 3 years ago . I'm currently trying to use an implementation of the sieve of erasthonese, but it still takes a very long time to find a long list of prime numbers. def sieve(n=1000000): not_prime = [] prime = [] for i in range(2, n+1): if i not in not_prime: prime.append(i) for j in range(i*i, n+1, i): not_prime.append(j) return prime[10002] I tried to hard code to what value the sieve should

VB programming. Integer array and prime numbers

心已入冬 提交于 2019-12-12 01:37:11
问题 I'm new to programming and stack overflow blogs, so hopefully I am following the 'do's and dont's' properly. I have been given an assignment question asking me to store 5 integers in an array and to determine if they are a prime number or not. The questions I have are as follows: How do I store them into an integer array? How do I make my program divide every input by every number less than the input? The code I have written so far is this: Sub Main() Dim a, b, c, d, e As Integer Dim isPrime

Recursive Function For Tracing Deletable Primes - Python 3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 23:08:01
问题 A deletable prime is a prime number that can have its digits removed in a certain order to always create primes, and eventually end up with a single digit prime. For example, 3301 is a deletable prime because it can be manipulated like this: 3301 -> 331 -> 31 -> 3 . I'm trying to write a recursive function in Python that traces the all paths of a deletable prime back to its single digit root(s) but I'm completely stuck. It has gotten way over my head and I can't even follow my own code

Prime number program in C

旧街凉风 提交于 2019-12-11 20:18:27
问题 This program is supposed to print the first x prime numbers, but I noticed that it was printing some non-prime numbers, such as 27 or 35. I've been looking at it for hours and nothing seems to pop up. So please, if you know what's wrong, tell me. #include <stdio.h> int main(){ int i=0, cont=2, prim=2, quant; printf("Insert number of prime numbers you wish: "); scanf("%d", &quant); printf("The first %d prime numbers are:\n", quant); while(i<quant){ if(prim%cont!=0 && (cont>1 && cont<prim)){

C program to take integer if it is Prime or Not Prime

淺唱寂寞╮ 提交于 2019-12-11 19:09:49
问题 I have this C program codes are completely and it's works very well, but not the correct result. I have them code source here in the below. Can anyone help me what is going on or what did I missing? Everyone are welcome to correct this English or code. #include <stdio.h> #include <stdlib.h> #include <math.h> int main (int argc, char *argv[]) { int n; int result; if (argc < 2) { printf ("Usage: p4 <number>\n"); } n = atoi (argv[1]); if (n < 2) { printf ("input number should be > 1\n"); }

How do I efficiently sieve through a selected range for prime numbers?

徘徊边缘 提交于 2019-12-11 18:23:00
问题 I've been working through Project Euler and Sphere Online Judge problems. In this particular problem, I have to find all the prime numbers within two given numbers. I have a function that looks promising (based on the Sieve of Eratosthenes), except it's too slow. Can someone spot what is slowing my function down so much, and hint at how I can fix it? Also, some comments about how to approach optimization in general (or links to such comments/books/articles etc,) would be greatly appreciated.

Determining if a number is prime in the above code [duplicate]

拟墨画扇 提交于 2019-12-11 18:19:05
问题 This question already has answers here : Program that checks if a number is prime number (5 answers) Prime numbers list generator immediately closing when run (2 answers) How come in this code to find prime numbers, is_prime(9) returns True? [duplicate] (2 answers) Closed 10 months ago . I have a simple problem: take a number and determine if it is prime. I cannot figure out why on earth the following does not work. def prime(x): for i in range (2, x): if x % i == 0: return False return True

JavaScript: Function not returning largest prime factor

泄露秘密 提交于 2019-12-11 18:05:47
问题 Input: 13195 Expected Result: 29 (largest prime factor of input) Actual Result: 2639 (largest factor of input, but not a prime number) I didn't bother with even numbers because the largest prime will either be 2 or some odd prime multiplied by 2 to get the input, so what's the point. function findPrimeFactor(num) { let prime; for (let factor = 3; factor < num; factor += 2) { if (num % factor === 0) { for (let i = 3; i < factor; i += 2) { if (factor % i === 0) { break; } else { prime = factor;

sum of primes and reciprocals of them and plot in matlab?

血红的双手。 提交于 2019-12-11 17:55:06
问题 Here is two codes in Mathematica to give the sum of primes up to n or up to n-th prime. ps2[n_]:= Sum[If[Element[p,Primes],p,0],{p,2,n}] or ps3[n_]:=Sum[1/Prime[i],{i,1,n}] or ps1[n_]:=Sum[If[Element[p,Primes],p,0],{p,2,n}] or ps[n_]:=Sum[Prime[i],{i,1,n}] Now I am looking for some code to do these sums and plot that in MATLAB , any idea? Thanks. 回答1: The first one is rather easy in Matlab: function result = ps(n) result = sum(primes(n)) (see PRIMES) 回答2: Using primes , as suggested by

Improve code to find prime numbers

蓝咒 提交于 2019-12-11 16:59:38
问题 I wrote this python code about 3 days ago, and I am stuck here, I think it could be better, but I don't know how to improve it. Can you guys please help me? # Function def is_prime(n): if n == 2 or n == 3: return True for d in range(3, int(n**0.5), 2): if n % d == 0: return False return True 回答1: A good deterministic way to find relatively small prime numbers is to use a sieve. The mathematical principle behind this technique is the following: to check if a number is prime, it is sufficient