Git Product home page Git Product logo

trial-division's Introduction

Trial-division

This simple Python program checks whether a given positive integer is a prime number using trial division

Description

The program consists of three main functions:

  • get_input(): Prompts the user to enter a positive integer. It ensures the input is valid and positive through a loop that continues until the input meets the conditions.

  • is_prime(n): Determines if the given number n is a prime. The function handles base cases (e.g., n <= 1 and n == 2), checks even numbers, and divides n by every odd number up to the square root of n.

  • main(): Controls the flow of the program, calling get_input() to retrieve the user's number and is_prime() to check its primality. The result is then printed.

How to Run

  1. Ensure you have Python installed on your computer
  2. Save the script in a file, for example prime_checker.py
  3. Open a terminal or command prompt
  4. Navigate to the directory where the script is saved
  5. Run the script using the command:
python trial_division.py
  1. Follow the on-screen prompts to input a positive integer

The script will then output whether the entered number is prime or not.

Features

  • Input validation to only accept positive integers
  • Efficient primality testing using trial division up to the square root of the input number

Feel free to modify and use this code as part of your own projects!

trial-division's People

Contributors

nakerthefirst avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.