primes

nth ugly number

情到浓时终转凉″ 提交于 2019-11-26 09:19:56
问题 Numbers whose only prime factors are 2, 3 or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large. I wrote a trivial program that computes if a given number is ugly or not. For n > 500 - it became super slow. I tried using memoization - observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that

Generate a list of primes up to a certain number

亡梦爱人 提交于 2019-11-26 09:00:36
问题 I\'m trying to generate a list of primes below 1 billion. I\'m trying this, but this kind of structure is pretty shitty. Any suggestions? a <- 1:1000000000 d <- 0 b <- for (i in a) {for (j in 1:i) {if (i %% j !=0) {d <- c(d,i)}}} 回答1: This is an implementation of the Sieve of Eratosthenes algorithm in R. sieve <- function(n) { n <- as.integer(n) if(n > 1e6) stop("n too large") primes <- rep(TRUE, n) primes[1] <- FALSE last.prime <- 2L for(i in last.prime:floor(sqrt(n))) { primes[seq.int(2L

What is a sensible prime for hashcode calculation?

蹲街弑〆低调 提交于 2019-11-26 08:45:30
问题 Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:) class HashTest { int i; int j; public int hashCode() { final int prime = 31; int result = prime + i; result = prime * result + j; return result; } } (If you have more attributes in the class, result = prime * result + attribute.hashCode(); is repeated for each additional attribute. For ints .hashCode() can be omitted.) This seems fine but for the choice 31 for the

The Sieve of Atkin

点点圈 提交于 2019-11-26 08:24:10
问题 I have been trying to learn algorithms for generating prime numbers and I came across Sieve of Atkin on Wikipedia. I understand almost all parts of the algorithm, except a few. Here are the questions: How are the three quadratic equations below formed? 4x^2+y^2, 3x^2+y^2 and 3x^2-y2 The algorithm in wikipedia talks about modulo sixty but I dont understand how/where that is used in the psudocode below. How are these reminders 1,5,7 and 11 found? Below is the pseudocode from Wikipedia for

Printing prime numbers from 1 through 100

谁都会走 提交于 2019-11-26 07:27:55
问题 This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don\'t think that\'s the way my book wants it to be written. It mentions something about square root of a number. So I did try changing my 2nd loop to for (int j=2; j<sqrt(i); j++) but it did not give me the result I needed. How would I need to change this code to the way my book wants it to be? int main () { for (int i=2; i<100; i++) for (int j=2; j<i; j++) { if

Speed up bitstring/bit operations in Python?

无人久伴 提交于 2019-11-26 07:23:58
问题 I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def prime_numbers(limit=1000000): \'\'\'Prime number generator. Yields the series 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ... using Sieve of Eratosthenes. \'\'\' yield 2 sub_limit = int(limit**0.5) flags = [False, False] + [True] * (limit - 2) # flags = BitString(limit) # Step

C - determine if a number is prime

被刻印的时光 ゝ 提交于 2019-11-26 06:50:27
I am trying to come up with a method that takes an integer and returns a boolean to say if the number is prime or not and I don't know much C; would anyone care to give me some pointers? Basically, I would do this in C# like this: static bool IsPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0 && i != number) return false; } return true; } OK, so forget about C. Suppose I give you a number and ask you to determine if it's prime. How do you do it? Write down the steps clearly, then worry about translating them into code. Once you have the algorithm determined, it will

Find n primes after a given prime number, without using any function that checks for primality

此生再无相见时 提交于 2019-11-26 06:48:12
问题 How to write a Program to find n primes after a given number? e.g. first 10 primes after 100, or first 25 primes after 1000. Edited: below is what I tried. I am getting output that way, but can we do it without using any primality-testing function? #include<stdio.h> #include<conio.h> int isprime(int); main() { int count=0,i; for(i=100;1<2;i++) { if(isprime(i)) { printf(\"%d\\n\",i); count++; if(count==5) break; } } getch(); } int isprime(int i) { int c=0,n; for(n=1;n<=i/2;n++) { if(i%n==0) c+

Calculating and printing the nth prime number

↘锁芯ラ 提交于 2019-11-26 06:38:12
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 program calculates the nth Prime number\n"); System.out.printf("Please enter the nth prime number you

Is a Recursive-Iterative Method Better than a Purely Iterative Method to find out if a number is prime?

拟墨画扇 提交于 2019-11-26 06:08:26
问题 I made this program in C that tests if a number is prime . I\'m as yet unfamiliar with Algorithm complexity and all that Big O stuff, so I\'m unsure if my approach, which is a combination of iteration and recursion , is actually more efficient than using a purely iterative method. #include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct primenode{ long int key; struct primenode * next; }primenode; typedef struct{ primenode * head; primenode * tail; primenode * curr; unsigned long