primes

Print series of prime numbers in python

一世执手 提交于 2019-11-26 05:29:24
问题 I am trying to learn Python programming, and I\'m pretty new at this. I was having issues in printing a series of prime numbers from one to hundred. I can\'t figure our what\'s wrong with my code. Here\'s what I wrote; it prints all the odd numbers instead of primes: for num in range(1,101): for i in range(2,num): if (num%i==0): break else: print(num) break 回答1: You need to check all numbers from 2 to n-1 (to sqrt(n) actually, but ok, let it be n). If n is divisible by any of the numbers, it

Python Prime number checker [duplicate]

荒凉一梦 提交于 2019-11-26 03:39:10
问题 This question already has an answer here: How to create the most compact mapping n → isprime(n) up to a limit N? 28 answers I have been trying to write a program that will take an inputed number, and check and see if it is a prime number. The code that I have made so far works perfectly if the number is in fact a prime number. If the number is not a prime number it acts strange. I was wondering if anyone could tell me what the issue is with the code. a=2 num=13 while num > a : if num%a==0 & a

Most elegant way to generate prime numbers [closed]

时光总嘲笑我的痴心妄想 提交于 2019-11-26 03:21:58
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . What is the most elegant way to implement this function: ArrayList generatePrimes(int n) This function generates the first n primes (edit: where n>1 ), so generatePrimes(5) will return an ArrayList with {2, 3, 5, 7, 11} . (I\'m doing this in C#, but I\'m happy with a Java

Program to find prime numbers

旧街凉风 提交于 2019-11-26 03:21:37
问题 I want to find the prime number between 0 and a long variable but I am not able to get any output. The program is using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication16 { class Program { void prime_num(long num) { bool isPrime = true; for (int i = 0; i <= num; i++) { for (int j = 2; j <= num; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.WriteLine ( \"Prime:\" + i ); } isPrime = true; } }

How to find prime numbers between 0 - 100?

本小妞迷上赌 提交于 2019-11-26 03:15:51
问题 In Javascript how would i find prime numbers between 0 - 100? i have thought about it, and i am not sure how to find them. i thought about doing x % x but i found the obvious problem with that. this is what i have so far: but unfortunately it is the worst code ever. var prime = function (){ var num; for (num = 0; num < 101; num++){ if (num % 2 === 0){ break; } else if (num % 3 === 0){ break; } else if (num % 4=== 0){ break; } else if (num % 5 === 0){ break; } else if (num % 6 === 0){ break; }

Recursive function causing a stack overflow

夙愿已清 提交于 2019-11-26 02:58:18
问题 I am trying to write a simple sieve function to calculate prime numbers in clojure. I\'ve seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am just trying to write a very simple (and slow) sieve. Here is what I have come up with: (defn sieve [potentials primes] (if-let [p (first potentials)] (recur (filter #(not= (mod % p) 0) potentials) (conj primes p)) primes)) For small ranges it works fine, but causes a stack overflow for large

A formula to find prime numbers in a loop

烂漫一生 提交于 2019-11-26 02:38:15
问题 I need to find prime numbers with for loop or while loop I wrote this but this is wrong <?php $i = 1; while($i<5) { for($j=1; $j<=$i; $j++) { if ($j != 1 && $j != $i) { echo $i . \"/\" . $j . \"=\" . $i%$j . \"<br />\"; if ($i%$j != 0) { echo $i . \"<br />\"; } } } echo \"<br />\"; $i += 1; } ?> Is there a way to divide a number with an array to find the remaining? 回答1: Here's a little function that I found: (http://icdif.com/computing/2011/09/15/check-number-prime-number/) Seemed to work for

isPrime Function for Python Language

China☆狼群 提交于 2019-11-26 02:30:37
问题 So I was able to solve this problem with a little bit of help from the internet and this is what I got: def isPrime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True But my question really is how to do it, but WHY. I understand that 1 is not considered a \"prime\" number even though it is, and I understand that if it divides by ANYTHING within the range it is automatically prime thus the return False statement. but my question is what role does the squaring the \"n\"

Explain this chunk of haskell code that outputs a stream of primes

喜欢而已 提交于 2019-11-26 02:14:42
问题 I have trouble understanding this chunk of code: let sieve (p:xs) = p : sieve (filter (\\ x -> x `mod` p /= 0) xs) in sieve [2 .. ] Can someone break it down for me? I understand there is recursion in it, but thats the problem I can\'t understand how the recursion in this example works. 回答1: It's actually pretty elegant. First, we define a function sieve that takes a list of elements: sieve (p:xs) = In the body of sieve , we take the head of the list (because we're passing the infinite list

Python Finding Prime Factors

好久不见. 提交于 2019-11-26 02:09:04
问题 Two part question: 1) Trying to determine the largest prime factor of 600851475143, I found this program online that seems to work. The problem is, I\'m having a hard time figuring out how it works exactly, though I understand the basics of what the program is doing. Also, I\'d like if you could shed some light on any method you may know of finding prime factors, perhaps without testing every number, and how your method works. Here\'s the code that I found online for prime factorization: n =