put "enter a number to determine if it is or is not prime"
get primenum
% for i : 1 .. primenum by 1
% end for
if (primenum / primenum) = 1 or primenum / 1 = 0 then
put primenum, " is a prime number"
else
put primenum, " is not a prime number"
end if
Run Code Online (Sandbox Code Playgroud)
输出说12是素数,这是错误的.我该如何修复此代码?
我想创建一个程序,将所有素数写入文件中(我知道有一个流行的算法"Sieve of Eratosthenes",但我试图自己制作).我正在尝试消除仍然具有值1的字节的所有复杂性,然后将它们写入文件中.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
void afficher_sur_un_ficher (FILE* ficher, int nb_bit);
char el_mask (int x);
int main()
{
FILE* p_fich;
char tab[4096], mask, eli_mask;
int nb_bit = 0, x, f;
for (int i = 0; i < 4096; i++)
{
tab[i] = 0xff;
}
for (int i = 0; i < 4096; i++)
{
for (mask = 0x01; mask != 0; mask <<= 1)
{
if ((tab[i] & mask) != 0)
{
x = nb_bit;
while …Run Code Online (Sandbox Code Playgroud) 在"没有恐惧的C++:让你感觉聪明的初学者指南"一书中,在第(2)章:决策,决定中,您可以看到这段代码作为素数程序的一部分:
while (i<=sqrt(static_cast<double>(n))
Run Code Online (Sandbox Code Playgroud)
假设"i"被初始化为"2",并且"n"是用户的输入.
为什么我们要比较"n"的"sqrt"而不是"n"呢?
谢谢.
对不起,我不得不问一个非常简单的问题.我的问题是我想生成素数直到最大数.
这是我的代码:
for (int i = 2; i <= max - 1; i++)
{
System.Threading.Thread.Sleep(1000);
if (Progress != null)
{
if (max%i == 0)
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码不起作用,我不知道为什么..
你能帮我么?
13195的主要因素是5,7,13和29. 600851475143中最大的素数是多少?
好的,所以我正在研究python中的项目euler问题3.我有点困惑.我不知道我在这个程序中得到的答案是否正确.如果somone可以请告诉我我做错了什么会很棒!
#import pdb
odd_list=[]
prime_list=[2] #Begin with zero so that we can pop later without errors.
#Define a function that finds all the odd numbers in the range of a number
def oddNumbers(x):
x+=1 #add one to the number because range does not include it
for i in range(x):
if i%2!=0: #If it cannot be evenly divided by two it is eliminated
odd_list.append(i) #Add it too the list
return odd_list
def findPrimes(number_to_test, list_of_odd_numbers_in_tested_number): # Pass in the prime …Run Code Online (Sandbox Code Playgroud) 嗨,伙计们,我正在制定一项计划,将所有素数的总和低于200万.这就是我所拥有的...我知道这个方法适用于寻找素数,因为我之前已经使用过它...但是当我运行这个程序时,我不断得到一个无限循环而没有输出......任何帮助都会很大不胜感激!
#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {
bool isPrime=true;
int i = 2;
int sum = 0;
do{
for ( int j = 2; j < i; j++)
{
if ( i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout << "Prime: " << i << endl;
sum += i; // add prime number to sum
}
i++;
}while(i < 2000000);
cout << "the sum of …Run Code Online (Sandbox Code Playgroud) 我想显示用户所需的素数.例如,如果用户想要第3个素数,我将显示5.我有以下java代码.
import java.util.Scanner;
public class Prime {
private static Scanner scanner;
public static void main(String args[]) {
//get input till which prime number to be printed
// System.out.println("Enter which prime number to be printed: ");
// scanner = new Scanner(System.in);
// int limit = scanner.nextInt();
int count = 0;
int number = 2;
//System.out.println("Printing prime number from 1 to " + limit);
while(count<=3)
{
if(isPrime(number)){
count++;
// System.out.println(count);
}
number++;
}
if(count == 3)
System.out.println("10001 prime is "+number);
}
public …Run Code Online (Sandbox Code Playgroud) 在标记为重复之前阅读:
我不想知道别人怎么做或者什么更快,我想通过自己来做.
我在计算的素数和实数(约1%)之间略有不同.我无法发现错误在哪里...
例如 :
从2到5万:
Wolfram | Alpha返回5 132,我的算法返回5 182
从2到500 000:
Wolfram | Alpha返回41 537,我的算法返回41 665
我认为我错了,Wolfram | Alpha是对的,所以这里是我的代码:
#include <QCoreApplication>
#include <QVector>
#include <QDebug>
QVector<int> tabPrime;
bool isPrime(int n)
{
bool boolIsPrime = true;
int i = 0;
while (boolIsPrime && tabPrime.at(i) * tabPrime.at(i) < n)
{
if (n % tabPrime.at(i) == 0)
boolIsPrime = false;
i++;
}
if(boolIsPrime)
tabPrime.append(n);
return boolIsPrime;
}
int main()
{
int numberWanted = 500000;
tabPrime.append(2);
tabPrime.append(3);
for(int i …Run Code Online (Sandbox Code Playgroud) 您好我已创建此程序以检查数字是否为素数.它有效,但出于某种原因说999是素数.我的错误在哪里 如果有人解释的话会很棒.谢谢!
这是我的计划:
number = raw_input('Enter a Number: ')
nnumber = int(number)
prime_range = range(2, nnumber)
for x in prime_range:
if nnumber % x == 0:
print 'Not a Prime Number!'
break
else:
print 'Prime Number!'
break
Run Code Online (Sandbox Code Playgroud) 我使用Java的Big Integers创建了一个Fermat素性测试.但是,尽管没有出现错误并且一切看起来都很好,但对于任何输入(BigInteger.valueOf(3)除外)都不会返回true或false.
public static boolean isPrime (BigInteger n){
BigInteger counter=BigInteger.ZERO;
boolean isPrime=false;
if(n.equals(BigInteger.valueOf(2)))isPrime=true;
if(n.compareTo(BigInteger.valueOf(2))>0 && n.compareTo(BigInteger.valueOf(40))<0) {
for (BigInteger a=BigInteger.valueOf(2);a.compareTo(n.subtract(BigInteger.ONE))<0;a.add(BigInteger.ONE)) {
if (a.modPow(n.subtract(BigInteger.ONE),n).equals(BigInteger.ONE)) counter.add(BigInteger.ONE);
}
if (counter.equals(n.subtract(BigInteger.valueOf(3)))) isPrime = true;
}
else {
for (BigInteger a=BigInteger.valueOf(2);a.compareTo(BigInteger.valueOf(40))<=0;a.add(BigInteger.ONE)) {
if (a.modPow(n.subtract(BigInteger.ONE),n).equals(BigInteger.ONE)) counter.add(BigInteger.ONE);
}
if (counter.equals(BigInteger.valueOf(39))) isPrime = true;
}
return isPrime;
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是由Big Biggers引起的吗?