primes

Prime Factors In C#

一笑奈何 提交于 2019-11-27 14:06:54
问题 I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor array things etc. just simple modulus. is there any code which fulfills what i desire? here is the code for finding simple factors, i need this code to be modified to calculate prime factors class Program { static void Main(string[] args) { int a, b; Console.WriteLine("Please enter your integer: "); a = int.Parse(Console

Arcane isPrime method in Java

早过忘川 提交于 2019-11-27 14:01:39
问题 Consider the following method: public static boolean isPrime(int n) { return ! (new String(new char[n])).matches(".?|(..+?)\\1+"); } I've never been a regular expression guru, so can anyone fully explain how this method actually works? Furthermore , is it efficient compared to other possible methods for determining whether an integer is prime? 回答1: First, note that this regex applies to numbers represented in a unary counting system, i.e. 1 is 1 11 is 2 111 is 3 1111 is 4 11111 is 5 111111 is

Problems with prime numbers

本小妞迷上赌 提交于 2019-11-27 13:19:20
问题 I am trying to write a program to find the largest prime factor of a very large number, and have tried several methods with varying success. All of the ones I have found so far have been unbelievably slow. I had a thought, and am wondering if this is a valid approach: long number = input; while(notPrime(number)) { number = number / getLowestDivisiblePrimeNumber(); } return number; This approach would take an input, and would do the following: 200 -> 100 -> 50 -> 25 -> 5 (return) 90 -> 45 ->

Parallel Algorithms for Generating Prime Numbers (possibly using Hadoop's map reduce)

£可爱£侵袭症+ 提交于 2019-11-27 13:17:43
Generating Prime numbers is a toy problem that I often attempt from time to time, especially when experimenting with a new programming language, platform or style. I was thinking of attempting to write a Prime Number Generation algorithm or a Prime Number Test Algorithm using Hadoop (Map Reduce). I thought I'd post this question to get tips, references, to algorithms, approaches. Although my primary interest is a Map Reduce based algorithm I wouldn't mind looking at new Hadoop programming models or for example looking at using PiCloud I have seems some interesting questions here on Prime

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

拥有回忆 提交于 2019-11-27 11:57:54
I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in increments of p and removes each of these numbers (p and multiples of p) Go to the next number in the list and repeat 2,3,4 Add unintentionally deleted prime numbers back to the list and this is what I have come up with: function eratosthenes(n){ var array = []; var tmpArray = []; // for containing unintentionally deleted elements like 2,3,5,7,... var

Fast Prime Number Generation in Clojure

白昼怎懂夜的黑 提交于 2019-11-27 11:18:54
I've been working on solving Project Euler problems in Clojure to get better, and I've already run into prime number generation a couple of times. My problem is that it is just taking way too long. I was hoping someone could help me find an efficient way to do this in a Clojure-y way. When I fist did this, I brute-forced it. That was easy to do. But calculating 10001 prime numbers took 2 minutes this way on a Xeon 2.33GHz, too long for the rules, and too long in general. Here was the algorithm: (defn next-prime-slow "Find the next prime number, checking against our already existing list" (

C#: How to make Sieve of Atkin incremental

非 Y 不嫁゛ 提交于 2019-11-27 09:22:35
I don't know if this is possible or not, but I just gotta ask. My mathematical and algorithmic skills are kind of failing me here :P The thing is I now have this class that generates prime numbers up to a certain limit: public class Atkin : IEnumerable<ulong> { private readonly List<ulong> primes; private readonly ulong limit; public Atkin(ulong limit) { this.limit = limit; primes = new List<ulong>(); } private void FindPrimes() { var isPrime = new bool[limit + 1]; var sqrt = Math.Sqrt(limit); for (ulong x = 1; x <= sqrt; x++) for (ulong y = 1; y <= sqrt; y++) { var n = 4*x*x + y*y; if (n <=

Why are primes important in cryptography?

心已入冬 提交于 2019-11-27 09:08:29
问题 One thing that always strikes me as a non-cryptographer: Why is it so important to use Prime numbers? What makes them so special in cryptography? Does anyone have a simple short explanation? (I am aware that there are many primers and that Applied Cryptography is the Bible, but as said: I am not looking to implement my own cryptographic algorithm, and the stuff that I found just made my brain explode - no 10 pages of math formulas please :)) Thanks for all the answers. I've accepted the one

Project Euler #10 Java solution not working

泄露秘密 提交于 2019-11-27 08:05:32
问题 I'm trying to find the sum of the prime numbers < 2,000,000. This is my solution in Java but I can't seem get the correct answer. Please give some input on what could be wrong and general advice on the code is appreciated. Printing 'sum' gives: 1308111344, which is incorrect. Edit: Thanks for all the help. Changed int to long and < to <= and it worked flawlessly, except for being an inefficient way of finding prime numbers :) /* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the

Determining if a number is prime

余生长醉 提交于 2019-11-27 07:30:53
I have perused a lot of code on this topic, but most of them produce the numbers that are prime all the way up to the input number. However, I need code which only checks whether the given input number is prime. Here is what I was able to write, but it does not work: void primenumber(int number) { if(number%2!=0) cout<<"Number is prime:"<<endl; else cout<<"number is NOt prime"<<endl; } I would appreciate if someone could give me advice on how to make this work properly. Update I modified it to check on all the numbers in a for loop. void primenumber(int number) { for(int i=1; i<number; i++) {