SVN / public / code / vorlesung-01 / Prime.cpp

Revision 154
Date2026-04-21T15:58:54+02:00
Committerhb1003
Download
// Copyright 2026, University of Freiburg
// Chair of Algorithms and Data Structures
// Author: Hannah Bast <bast@cs.uni-freiburg.de>

// Function that computes whether the given number `n` is prime. If prime,
// return 0, otherwise return the smallest divisor greater than 1.
int isPrime(int n) {
  if (n == 1) {
    return 1;
  }
  int i = 2;
  while (i * i <= n) {
    if (n % i == 0) {
      return i;
    }
    i = i + 1;
  }
  return 0;
}