primes

Relatively Prime Numbers

删除回忆录丶 提交于 2019-12-04 10:46:10
How to make a function in c++ to determine if two entered numbers are relatively prime (no common factors)? For example "1, 3" would be valid, but "2, 4" wouldn't. TonyK Galvanised into action by Jim Clay's incautious comment, here is Euclid's algorithm in six lines of code: bool RelativelyPrime (int a, int b) { // Assumes a, b > 0 for ( ; ; ) { if (!(a %= b)) return b == 1 ; if (!(b %= a)) return a == 1 ; } } Updated to add: I have been out-obfuscated by this answer from Omnifarious , who programs the gcd function thus: constexpr unsigned int gcd(unsigned int const a, unsigned int const b) {

Generator function for prime numbers [duplicate]

拥有回忆 提交于 2019-12-04 10:24:32
This question already has answers here : Simple Prime Generator in Python (23 answers) Closed 5 years ago . I'm trying to write a generator function for printing prime numbers as follows def getPrimes(n): prime=True i=2 while(i<n): for a in range(2,i): if(i%a==0): prime=False break if(prime): yield i However I'm not getting the desired results p=getPrimes(100) should give me a generator function that will iterate primes from 2 through 100 but the result I'm getting is [2,3]. What am I doing wrong? You need to reset prime to True as the first statement inside the while block, not before it. As

Prime test, 2-digit numbers

懵懂的女人 提交于 2019-12-04 10:08:44
I want to print all prime numbers that are 2-digits long. Here is my code: for(int input = 11; input <= 99; input += 2){ for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){ if(input%x != 0){ System.out.println(input); break; }else{ break; } } } The problem is that it prints numbers like 35 or 49 which are not prime numbers. This works. You can see the output here . public class Main { public static void main(String[] args) { for(int input = 11; input <= 99; input += 2){ boolean found = false; for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){ if(input%x == 0){ found = true; break; } } if(

Project Euler 3 - Why does this method work?

99封情书 提交于 2019-12-04 09:23:37
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? I solved this problem on Project Euler my own way, which was slow, and then I found this solution on someone's github account. I can't figure out why it works. Why are a number of factors removed, equal to an index? Any insight? def Euler3(n=600851475143): for i in range(2,100000): while n % i == 0: n //= i if n == 1 or n == i: return i This function works by finding successive factors of its input. The first factor it finds will necessarily be prime. After a prime factor is found, it

Number of distinct prime partitions [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 09:12:56
This question already has answers here : Closed 6 years ago . Possible Duplicate: A number as it’s prime number parts I have this homework assignment of mine, hard as hell, where I have to get all the distinct prime partitions of a given number. For example, number 7 has five different prime partitions (or five different ways of representing the 2 prime partitions it has): 5 + 2 2 + 5 3 + 2 + 2 2 + 3 + 2 2 + 2 + 3 As you can see, the number itself is excluded in the case it's a prime. I don't have to print all the distinct partitions, only the number of them. So I'm a bit lost with this. I've

Python, prime number checker [duplicate]

烈酒焚心 提交于 2019-12-04 06:41:05
问题 This question already has answers here : Simple Prime Generator in Python (23 answers) Closed 6 years ago . Hi i'm making a function that checks if a number is a prime or not but its telling me that 9 is a prime. def eprimo(num): if num < 2: return False if num == 2: return True else: for div in range(2,num): if num % div == 0: return False else: return True 回答1: You're going to return from the first iteration of that for loop whether you're done checking or not. You shouldn't return from

C++ Vector Elements Count

霸气de小男生 提交于 2019-12-04 06:04:19
问题 In C++, using the vector header, how do I find the number of elements? #include <iostream> #include <cmath> #include <fstream> #include <cstdlib> #include <vector> using namespace std; int primer(int max); int main() { system("pause"); return 0; primer(1000); } int primer(int max){ vector<int> a; a[1]=2; for (int i=2;i<=max;i++){ bool prime=true; for (int ii=1;ii<=#a;ii++) { if i/a[ii]==math.floor(i/a[ii]) { prime=false; } } if prime==true { a[#a+1]=i; } } for (i=1;i<=#a;i++) { cout << a[i]);

how to represent a number as a sum of 4 primes?

痴心易碎 提交于 2019-12-04 05:21:37
Here is the problem ( Summation of Four Primes ) states that : The input contains one integer number N (N<=10000000) in every line. This is the number you will have to express as a summation of four primes Sample Input: 24 36 46 Sample Output: 3 11 3 7 3 7 13 13 11 11 17 7 This idea comes to my mind at a first glance Find all primes below N Find length of list (.length = 4) with Integer Partition problem (Knapsack) but complexity is very bad for this algorithm I think. This problem also looks like Goldbach's_conjecture more. How can I solve this problem? This problem has a simple trick. You

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

落爺英雄遲暮 提交于 2019-12-04 05:12:09
问题 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

How to fix - 41: non-static variable cannot be referenced from a static context -> What is the reason for this?

最后都变了- 提交于 2019-12-04 05:03:06
问题 I'm trying to write this code to get the first initialCapacity prime numbers and then print them in sequence using java. It isn't working for two reasons, firstly I get the error 41: non-static variable listOfPrimeNumbers cannot be referenced from a static context when I try to run the program, but even when I change the variable to static and run the program, it will only print out "1". So it is only iterating the while loop in the constructor Primes once, and then stopping and I simply