primes

Calculating whether number is prime in Prolog

☆樱花仙子☆ 提交于 2019-12-11 03:38:47
问题 I am trying to calculate if the input is a prime number but something goes wrong... here's my code: primeNumber(X):- prime_prime(A, 1). prime_prime(A, B):- R is A mod B, R =:= 1, R =:= A. prime_prime(X, B):- B < A, Next is B + 1, prime_prime(A, Next). It gives me false every time. Anyone got any clues or idea on what I am doing wrong? 回答1: See http://www.swi-prolog.org/pldoc/man?function=mod/2: +IntExpr1 mod +IntExpr2 Modulo, defined as Result = IntExpr1 - (IntExpr1 div IntExpr2) × IntExpr2,

Is there a way to “grow” my array as my program continues?

你。 提交于 2019-12-11 03:28:09
问题 I am creating a prime generator in c++ using an array to store primes as I find them and then use them to check against potentials later. Is there a way to "grow" my array as my program continues? 回答1: See std:vector. http://new.cplusplus.com/reference/stl/vector/ 回答2: I would use a std:vector something like this: vector<int> primes; then you would add primes to it by using: primes.push_back(2); and retrieve values from the vector by using: primes[0]; Good luck! 回答3: You could use a std:

deciding if a number is perfect or prime

旧街凉风 提交于 2019-12-11 02:37:49
问题 the problem is : "Write a function to find out if a number is a prime or perfect number." so far i have worked on the perfect part first and this is what i have: #include <iostream> using namespace std; bool perfectNumber(int); int main() { int number; cout<<"Please enter number:\n"; cin>>number; bool perfectNumber(number); return 0; } bool perfectNumber(int number) { int i; int sum=0; for(i=1;i<=number/2;i++) { if(number%i==0) { sum+=i; } } if (sum==number) return i; else return 0; } HOWEVER

Calculate all possible factors of a prime

浪子不回头ぞ 提交于 2019-12-11 01:38:05
问题 Was going through a Python Tutorial and came across an example to check whether a number is prime or not. I changed a few things so that the result would display a list of all possible factors of the number, if the number is not a prime, However, the code didn't work. Code: def isprime(number): print "Reticulating Splines..." fnum=[1,] notprime=0 for p in range(2, number): if (number % p) == 0: notprime=1 fnum.append(p) continue if notprime == 1: return number, "is not a prime because of

SPOJ PRIME1 : TLE [closed]

假如想象 提交于 2019-12-11 01:27:57
问题 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 . I tried implementing the segmented sieve algorithm for this [question]:http://www.spoj.pl/problems/PRIME1/ as follows : #include <iostream> #include <string> #include <set> #include<math.h> #include<vector> #include<cstdlib> #include<cstdio> #include<cstring> #include<cstdio> #define MAX 32000 // sqrt of the

How to check for prime numbers

送分小仙女□ 提交于 2019-12-10 23:35:46
问题 The following code lists all prime numbers from 1 to 10: Dim primeN As Integer Dim primeI As Integer Dim primeFlag As Boolean For primeN = 1 To 10 primeFlag = True For primeI = 2 To primeN / 2 If primeN Mod primeI = 0 Then primeFlag = False End If Next If primeFlag Then Console.WriteLine(primeN) End If Next Console.ReadLine() How can I change it so that it reads the user input and then checks if it's a prime number? 回答1: You're almost there. Instead of a loop, just get the user input and put

How can I find the number of ways a number can be expressed as a sum of primes? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-10 21:20:52
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Generating the partitions of a number Prime number sum The number 7 can be expressed in 5 ways as a sum of primes: 2 + 2 + 3 2 + 3 + 2 2 + 5 3 + 2 + 2 5 + 2 Make a program that calculates, in how many ways number n can be expressed as a sum of primes. You can assume that n is a number between 0-100. Your program should print the answer in less than a second Example 1: Give number: 7 Result: 5 Example 2: Give

Optimize calculation of prime numbers [duplicate]

让人想犯罪 __ 提交于 2019-12-10 19:33:58
问题 This question already has answers here : Project Euler 3 - Highest Prime Factor (2 answers) Project Euler #3 takes forever in Java (9 answers) Closed 6 years ago . I am trying Problem 3 from Project Euler and my algorithm is far too slow. Does anyone know how to optimize this? The number I am trying to calculate up to is 600851475143L. It takes forever to calculate this so I need a way to speed up the calculations. LOGIC: Go through all the numbers from 3 until that number-1 For each of these

Why does this `else` block work yet it is not on the same level as the `if` case? [duplicate]

本小妞迷上赌 提交于 2019-12-10 17:48:33
问题 This question already has answers here : Why does python use 'else' after for and while loops? (21 answers) Closed 2 years ago . This code runs pretty well and generates the wanted list of prime numbers. But the else block that prints our prime numbers is out of block, but it works anyway, can someone explain it to me? for num in range(0, 100 + 1): # prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num) Reference: programiz.com 回答1:

How can I write a fast function to calculate total divisors of a number?

巧了我就是萌 提交于 2019-12-10 17:38:58
问题 I have to find the the total number of divisors of a given number N where can be as large as 10^14 .I tried out calculating the primes upto 10^7 and then finding the the divisors using the exponents of the prime factors.However it is turning out to be too slow as finding the primes using the sieve takes 0.03 s. How can I calculate the total number of divisors faster and if possible without calculating the primes? Please pseudo code /well explained algorithm will be greatly appreciated. 回答1: