“nombre premier” Réponses codées

nombre premier

def is_prime(n):
  if(n<0):
    print("n is not a nature number!")
    return False
  for i in range(1,ceil(n/2)+1):
    if (i != n and i != 1 and n%i == 0):
      return False
  return True
print(is_prime(int(input("number>>"))))
Silver Takana

nombre premier

number = int(input("please enter a number: "))


lis = [2, 3, 5, 7] # list of first 4 prime numbers

lis2 = [i for i in lis if number % i == 0] # if the number is prime, this list (lis 2) will be empty or that it's one of the first elements in lis which are prime numbers

if len(lis2) == 0 or number in lis:
    print("Number is prime!")
else:
    print("Number is NOT prime!")
Defeated Donkey

Nombres premiers

using System;
public class Program
{
    static void Main(string[] args)
    {
        var results = GenerateSieve(1000);
        var i=0;
        foreach (var item in results)
        {
            if(item) Console.Write(i + " ");
            i++;
        }
    }
 
    static bool[] GenerateSieve(int num)
    {
        // Creating an array indicating whether numbers are prime.
        bool[] isPrime = new bool[num + 1];
        for (int i = 2; i <= num; i++) isPrime[i] = true;
 
        // Removing out multiples.
        for (int i = 2; i <= num; i++)
        {
            // Check if i is prime.
            if (isPrime[i])
            {
                // Eliminate multiples of i.
                for (int j = i * 2; j <= num; j += i)
                    isPrime[j] = false;
            }
        }
        return isPrime;
    }
}
PrashantUnity

nombre premier

import java.util.Scanner;

public class Prime {
    public static void main (String[] args) {
        Scanner userInput = new Scanner(System.in);
        int n = userInput.nextInt();
        if (!(n % 2 == 0) || n == 2) {
            System.out.println("not prime");
        } else {
            System.out.println("prime");
        }
    }
}
Breakable Boar

Réponses similaires à “nombre premier”

Questions similaires à “nombre premier”

Plus de réponses similaires à “nombre premier” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code