primes

Determining if a BigInteger is Prime in Java

旧时模样 提交于 2019-12-25 07:21:15
问题 I am trying hands on validation of whether a BigInteger number entered is a Prime Number or not! But, it is running fine for smaller numbers like 13,31 ,but it yields error in the case of 15 ;by declaring it as a Prime. I am unable to figure-out the mistake,probably it is hidden in the squareroot() method approach involving binary-search ! Please view the code and help me point out the mistake!!! Calling code :- boolean p=prime(BigInteger.valueOf(15)); System.out.println("P="+p); Called code

How to check unknown logic in Verilog?

萝らか妹 提交于 2019-12-25 04:24:31
问题 I'm checking primality of a number in a form of 6n+1 or 6n-1. I have the below code, but it doesn't seem to be generated correct result. module prime(clk, rst, start, A, ready, P); input clk, rst, start; input [7:0] A; output ready, P; reg ready, P; reg [7:0] divisor; reg val; always @ (posedge clk or negedge rst) begin if (!rst) begin P <= 1'bx; end else if (start) begin case (A) -1 : P <= 1; 0 : P <= 1; 1 : P <= 1; 2 : P <= 1; 3 : P <= 1; 5 : P <= 1; endcase if (A%2 == 0 && A != 2) begin P

Calculating sum of prime number below 2 million by using Sieve of Atkin

喜你入骨 提交于 2019-12-25 02:53:53
问题 I'm doing project Euler and I got this problem. I run the code in VS 2013 and the program crashes because of overflow. This is my method: void problem10() { long long int iter = 2, sum = 0; //Sieve of Atkin bool isPrime[PRIME_LIMIT+1]; for (long long int i = 5; i <= PRIME_LIMIT; i++) { isPrime[i] = false; } long long int lim = ceil(sqrt(PRIME_LIMIT)); for (long long int x = 1; x <= lim; x++) { for (long long int y = 1; y <= lim; y++) { long long int n = 4 * x*x + y*y; if (n <= PRIME_LIMIT &&

Find prime numbers in range

坚强是说给别人听的谎言 提交于 2019-12-25 02:26:17
问题 def is_prime(number): for i in range(2, number): if number % 1 == 0 and number % i == 0: return False else: return True print(is_prime(13)) print(is_prime(55)) #True ##Why ??? def prime_numbers(a, b): lst = [] for i in range(a,b+1): if is_prime(i): lst.append(i) return lst print(prime_numbers(50, 100)) This is my code and suppose I have prime_numbers(50, 100) . It should return [53, 59, 61, 67, 71, 73, 79, 83, 89, 97] instead of [51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81,

Relation between a number and position of smallest prime larger than its square root in the list of primes [closed]

允我心安 提交于 2019-12-25 00:27:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . While making a list of prime numbers, one needs to check if a number is divisible by any prime equal to or smaller than its square root. However, instead of checking every time whether a prime number is more than square root of number being checked (as done on this page: Optimize prime number finder and cpu

Understanding this Ruby Program to find prime numbers

烂漫一生 提交于 2019-12-24 19:15:11
问题 I am new to Ruby, and I have been reading over this program that finds us prime numbers. This example came in a textbook, after talking about loops (while & until) it showed this example. I find it pretty confusing. What is the purpose of the prime_flag? Why is J set as 2? (j <= i / 2) -- This one I don't understand. j = j + 1 -- what is the purpose of this. My apologies for the long question, but any help is greatly appreciated. Please and thank you. # Initialize our counter i = 1 # i: [0,

Recursion in a prime generator

不想你离开。 提交于 2019-12-24 17:23:38
问题 I'm making a prime generator, and to make it more efficient, i'm trying to only test numbers against primes that I've already found rather than all numbers < sqrt of the number being tested. I'm trying to get a to be my list of primes, but i'm not sure how to make it recur inside my second for loop. I think this is only testing against a <- 2 and not a <- c(a,i) x <- 3:1000 a <- 2 for (i in x) {for (j in a) {if (i %% j == 0) {next} else {a <- unique(c(a,i))}}} a 回答1: A non-recursive mod using

Recursion in a prime generator

痞子三分冷 提交于 2019-12-24 17:23:26
问题 I'm making a prime generator, and to make it more efficient, i'm trying to only test numbers against primes that I've already found rather than all numbers < sqrt of the number being tested. I'm trying to get a to be my list of primes, but i'm not sure how to make it recur inside my second for loop. I think this is only testing against a <- 2 and not a <- c(a,i) x <- 3:1000 a <- 2 for (i in x) {for (j in a) {if (i %% j == 0) {next} else {a <- unique(c(a,i))}}} a 回答1: A non-recursive mod using

Finding composite numbers

僤鯓⒐⒋嵵緔 提交于 2019-12-24 12:39:02
问题 I have a range of random numbers. The range is actually determined by the user but it will be up to 1000 integers. They are placed in this: vector<int> n and the values are inserted like this: srand(1); for (i = 0; i < n; i++) v[i] = rand() % n; I'm creating a separate function to find all the non-prime values. Here is what I have now, but I know it's completely wrong as I get both prime and composite in the series. void sieve(vector<int> v, int n) { int i,j; for(i = 2; i <= n; i++) { cout <<

C# - Need help creating program that simulates Prime Number behavior by drawing lines

六月ゝ 毕业季﹏ 提交于 2019-12-24 12:17:24
问题 I challenged myself to create a program to experiment with Prime Numbers, I already have an idea how to do it, but not the coding skills.. My plan is this: First ill create a program in C# that makes straight lines that follow some rules: rule 1: all lines have the same length. rule 2: all lines are either horizontal or vertical (no diagonals). rule 3: Every new line begins where the previous line has ended (that way all the lines are joined). Now for the tricky part: I would like to make a