primes

Need a hint/advice as to how to factor very large numbers in JavaScript

守給你的承諾、 提交于 2019-12-02 06:39:11
My task is to produce an array containing all the prime numbers up to a 12-digit number. I tried to emulate the Sieve of Eratosthenes by first making a function enumerate that produces an array containing every integer from 2 to num : var enumerate = function(num) { array = []; for (var i = 2; i <= num; i++) { array.push(i); } return array; }; Then I made a function leaveOnlyPrimes which loops through and removes multiples of every array member up to 1/2 max from the array (this does not end up being every integer because the array become smaller with every iteration): var leaveOnlyPrimes =

program to print series of prime numbers using java

佐手、 提交于 2019-12-02 06:19:14
This code is to print the series of prime number up to given limit but when I am trying to execute this,it goes into infinite loop. import java.io.*; class a { public static void main(String s[]) throws IOException { int count=1; String st; System.out.println("how many prime no. do you want"); BufferedReader obj= new BufferedReader (new InputStreamReader (System.in)); st=obj.readLine(); int n=Integer.parseInt(st); while(count!=n) { int num=2; for(int i=2;i<num;i++) { if(num%i==0) { count++; break; } } num++; } } } USe isPrime(int num) method in loop to generate prime numbers example is show

How can I improve this code for Project Euler 7?

女生的网名这么多〃 提交于 2019-12-02 03:58:08
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? My solution: public class Prime_Number { public static boolean isPrime(long n) { if ((n > 2 && n % 2 == 0) || (n > 3 && n % 3 == 0) || (n > 5 && n % 5 == 0) || n == 0 || n == 1) { return false; } return true; } public static void main(String[] args) { int count = 0; int prime = 0; while (prime <= 10001) { if (isPrime(count) == true) { prime++; if (prime == 10001) { System.out.println(count + " is a prime number" + "(" + prime + ")"); } } count++; } } } But it

Circular prime numbers

▼魔方 西西 提交于 2019-12-02 03:08:14
I am trying to convert the following function which test the number if it's prime to another one that test if the integer is a circular prime. eg. 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. So i need to rotate the digits of the integer and test if the number is prime or not. any ideas? note: I am new to Haskell Programming isPrime :: Integer -> Bool isPrime 1 = False isPrime 2 = True isPrime n | (length [x | x <- [2 .. n-1], n `mod` x == 0]) > 0 = False | otherwise = True isCircPrime :: Integer -> Bool You can improve the efficiency and elegance of your isPrime

How to find prime factors of a number in c++?

别来无恙 提交于 2019-12-02 02:59:55
问题 I am attempting project euler question number 3, and I don't get the desired result. My logic: List all the factors of the number 13195 and save them in an array. Check if each number in the array is a prime. If the number is found to be prime save it in an other array. display the contents of the second array. Hope it contains only prime factors. RESULT: The first array contains all the factors as expected, The second I think duplicates the first array or slips in some non-primes, Please

How to find prime factors of a number in c++?

﹥>﹥吖頭↗ 提交于 2019-12-02 02:41:36
I am attempting project euler question number 3, and I don't get the desired result. My logic: List all the factors of the number 13195 and save them in an array. Check if each number in the array is a prime. If the number is found to be prime save it in an other array. display the contents of the second array. Hope it contains only prime factors. RESULT: The first array contains all the factors as expected, The second I think duplicates the first array or slips in some non-primes, Please help! :) My code: #include <iostream> using namespace std; long int x,y=2; long int number=13195; long int

Python recursive program to prime factorize a number

风流意气都作罢 提交于 2019-12-02 02:03:02
问题 I wrote the following program to prime factorize a number: import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) print li #First print statement; This is what is returned return li prime_factorize(x/i,li) if __name__=='__main__': print prime_factorize(300) #Second print statement, WTF. why is this None Following is the output I get: [2, 2, 3, 5, 5] None Altho', the returned

Check for a prime number using recursive helper function

牧云@^-^@ 提交于 2019-12-02 00:25:22
I am trying to check if a number is prime using recursion. I was required to use a recursive helper function, but I am not sure how I should implement it. I think I know the algorithm, but I've never tried to use a recursive helper function in Racket. This is my current thoughts: See if n is divisible by i = 2 Set i = i + 1 If i^2 <= n continue. If no values of i evenly divided n , then it must be prime. This is what I have so far... (define (is_prime n) (if (<= n 1) #f (if (= (modulo n 2) 0) #f ) What would be a good approach using a recursive helper function?? Thanks! Using a helper simply

Python recursive program to prime factorize a number

社会主义新天地 提交于 2019-12-02 00:10:08
I wrote the following program to prime factorize a number: import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) print li #First print statement; This is what is returned return li prime_factorize(x/i,li) if __name__=='__main__': print prime_factorize(300) #Second print statement, WTF. why is this None Following is the output I get: [2, 2, 3, 5, 5] None Altho', the returned value is printed properly, the after returned value seems to be printing none, all the time. What am I

Circular prime numbers Incorrect Output Python Program

老子叫甜甜 提交于 2019-12-02 00:05:37
问题 Problem Statement : The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? My Problem I have checked through all the code and found that the binary search function is giving a return 1 statement as the output print success. But nothing is added to the final list. Please Help