Answers for "find prime number"

1

find is number prime

int number;
cin >> number;
if (number == 2) {
	cout << "Prime" << endl;
	return;
}else if(number == 1 || number % 2 == 0){
	cout << "Not prime" << endl;
	return;
}
for (int i = 3; i * i <= number; i+=2) {
	if(number % j == 0){
		cout << "Not prime" << endl;
		return;
	}
}
cout << "Prime" << endl;
return;
Posted by: Guest on June-17-2021
0

program to know if a number is prime

#include<bits/stdc++.h>
using namespace std;
bool Is_Prime(long long x){
	if(x%2==0)return false;
	for(int i=3;i*i<=x;i+=2)
		if(x%i==0)return false;
	return true;
}
int main(){
	long long x;
	cin>>x;
	if(Is_Prime(x))cout<<"Is Prime";
	else cout<<"Is not Prime";
}
Posted by: Guest on April-30-2020
0

Find primes in numbers

// Find primes in numbers

fn prime_factors(n: i64) -> String {
  let mut n = n as u64;
  let mut d = 2;
  let mut mem = std::collections::BTreeMap::new();
  while d <= n {
    if n % d == 0 {
      n /= d;
      let old = mem.entry(d).or_insert(0);
      *old += 1;
    } else {
      d += 1;
    }
  }
  mem.iter().map(|(key, val)| match *val {
    1 => format!("({})", key),
    _ => format!("({}**{})", key, val),
  }).collect::<String>()
}

fn main() {
    println!("Primes in numbers = {} ", prime_factors(7775460));
    println!("Primes in numbers = {} ", prime_factors(13*17*3*26));
    println!("Primes in numbers = {} ", prime_factors(14));
}
Posted by: Guest on October-08-2021
0

Find primes in numbers

// Find primes in numbers

package main

import "fmt"

func PrimeFactors(n int) (out string) {
  t, d := 0, 2
  for {
    if n % d != 0 {
      if t == 1 { out += fmt.Sprintf("(%d)", d) }
      if t >  1 { out += fmt.Sprintf("(%d**%d)", d, t) }
      if n == 1 { break }
      t = 0; d++; continue
    }
    n /= d
    t++
  }
  return
}

func main() {
    fmt.Printf("Sum of x + y = %s\n", PrimeFactors(7775460))   // (2**2)(3**3)(5)(7)(11**2)(17) 
    fmt.Printf("Sum of x + y = %s\n", PrimeFactors(13*17*3*26))
    fmt.Printf("Sum of x + y = %s\n", PrimeFactors(14))
}
Posted by: Guest on October-08-2021
0

what is prime number

Number which is only divisible by itself and 1(eg. 2,3...)
Posted by: Guest on August-12-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language