primes

finding sum of prime numbers under 250

我的未来我决定 提交于 2019-11-28 05:09:20
问题 var sum = 0 for (i = 0; i < 250; i++) { function checkIfPrime() { for (factor = 2; factor < i; factor++) { if (i % factor = 0) { sum = sum; } else { sum += factor; } } } } document.write(sum); I am trying to check for the sum of all the prime numbers under 250. I am getting an error saying that i is invalid in the statement if (i % factor = 0) I know was creating in the original for statement, but is there any way to reference it in the if statement? 回答1: With the prime computation, have you

A number as it's prime number parts

爱⌒轻易说出口 提交于 2019-11-28 04:37:16
问题 I have to print the number of ways you can represent a given number as it's prime number parts. Let me clarify: Let's say I have been given this number 7. Now, first of all, I have to find all the prime numbers that are less than 7, which are 2, 3 and 5. Now, in how many ways can I summarize those numbers (I can use one number as many times I want) so that the result equals 7? For example, number 7 has five ways: 2 + 2 + 3 2 + 3 + 2 2 + 5 3 + 2 + 2 5 + 2 I'm totally lost with this task. First

Efficient storage of prime numbers

强颜欢笑 提交于 2019-11-28 03:42:58
For a library, I need to store the first primes numbers up to a limit L. This collection must have a O(1) lookup time (to check whether a number is prime or not) and it must be easy, given a number, to find the next prime number (assuming it is smaller than L). Given that L is fixed, an Eratostene sieve to generate the list is fine. Right now, I use a packed boolean array to store the list, which contains only entries for odd numbers between 3 and L (inclusive). This takes (L-2)/2 bits of memory. I would like to be able to statically increase L without using more memory. Is there a data

How does this regex find primes? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-28 02:59:08
Possible Duplicate: How to determine if a number is a prime with regex? This page claims that this regular expression discovers non-prime numbers (and by counter-example: primes): /^1?$|^(11+?)\1+$/ How does this find primes? Matchu I think the article explains it rather well, but I'll try my hand at it as well. Input is in unary form. 1 is 1 , 2 is 11 , 3 is 111 , etc. Zero is an empty string. The first part of the regex matches 0 and 1 as non-prime. The second is where the magic kicks in. (11+?) starts by finding divisors. It starts by being defined as 11 , or 2. \1 is a variable referring

How to check if the value in a text box is prime or not with jQuery

怎甘沉沦 提交于 2019-11-28 02:15:06
问题 I am trying to determine whether the value in a text box is prime or not using jQuery. Here is what I've tried so far, but it's not working: $("#textbx").keyup(function(){ if ($("#textbx").val().length > 0) { $("#btn").removeAttr('disabled'); } }); $("#textbx").blur(function(){ if ($("#textbx").val().length ==0) { $("#btn").attr('disabled','disabled'); } }); $("#textbx").keypress(function (e) { if (e.which!=8 && (e.which < 48 || e.which > 57)) { $("#msg").html("plz press nimber only").show()

Find prime numbers using Scala. Help me to improve

随声附和 提交于 2019-11-28 00:17:57
I wrote this code to find the prime numbers less than the given number i in scala. def findPrime(i : Int) : List[Int] = i match { case 2 => List(2) case _ => { val primeList = findPrime(i-1) if(isPrime(i, primeList)) i :: primeList else primeList } } def isPrime(num : Int, prePrimes : List[Int]) : Boolean = prePrimes.forall(num % _ != 0) But, I got a feeling the findPrime function, especially this part: case _ => { val primeList = findPrime(i-1) if(isPrime(i, primeList)) i :: primeList else primeList } is not quite in the functional style. I am still learning functional programming. Can anyone

Prime Number Formula

南笙酒味 提交于 2019-11-27 23:23:30
I am trying to write a prime number function in C# and I am wondering if the follow code will work. It "appears" to work with the first 50 numbers or so. I just want to make sure it will work no matter how big the number is: static bool IsPrime(int number) { if ((number == 2) || (number == 3) || (number == 5) || (number == 7) || (number == 9)) return true; if ((number % 2 != 0) && (number % 3 != 0) && (number % 5 != 0) && (number % 7 != 0) && (number % 9 != 0) && (number % 4 != 0) && (number % 6 != 0)) return true; return false; } No it won't work! Try 121 = 11 * 11 for example which obviously

How does this regular expression work?

你离开我真会死。 提交于 2019-11-27 21:21:35
From this article , /^1?$|^(11+?)\1+$/ checks whether a number(its value in unary) is prime or not. Using this, perl -l -e '(1 x $_) !~ /^1?$|^(11+?)\1+$/ && print while ++$_;' returns a list of prime numbers. I do not have enough experience with Perl, but what I understand is that the regular expression will be true for a number that is not prime. So, if we print all numbers that do not produce a true with this expression, we have a list of prime numbers. Thats what the perl query is trying to do. About the regex part, ^1?$ part is for counting 1 as not prime ^(11+?)\1+$ is for matching not

Very simple prime number test - I think I'm not understanding the for loop

走远了吗. 提交于 2019-11-27 20:59:49
问题 I am practicing past exam papers for a basic java exam, and I am finding it difficult to make a for loop work for testing whether a number is prime. I don't want to complicate it by adding efficiency measures for larger numbers, just something that would at least work for 2 digit numbers. At the moment it always returns false even if n IS a prime number. I think my problem is that I am getting something wrong with the for loop itself and where to put the "return true;" and "return false;"...

Factor a large number efficiently with gmp

纵然是瞬间 提交于 2019-11-27 18:33:02
问题 I need to get all the prime factors of large numbers that can easily get to 1k bits. The numbers are practically random so it shouldn't be hard. How do I do it efficiently? I use C++ with GMP library. EDIT: I guess you all misunderstood me. What I mean by prime a number is to get all prime factors of the number. Sorry for my english, in my language prime and factor are the same :) clarification (from OP's other post): What I need is a way to efficiently factor(find prime factors of a number)