**prime**numbers less than or equal to k? (using python 3)

k = int (input ('Enter an integer...')) n = 0 while n<=k: t = 0 n = n + 1 for x in range(1,n + 1): if n%x == 0: t = t + x if x == n: if t == 2*n: print (n)

Posted 18 October 2012 - 04:24 AM

Hey, so I have this code that will generate all perfect numbers less than or equal to k. How would I alter this code to generate all **prime** numbers less than or equal to k? (using python 3)

k = int (input ('Enter an integer...')) n = 0 while n<=k: t = 0 n = n + 1 for x in range(1,n + 1): if n%x == 0: t = t + x if x == n: if t == 2*n: print (n)

Posted 18 October 2012 - 04:32 AM

If it doesn't have to be efficient, you can write a function that checks whether a number is primer and then call that function for all numbers from 1 to k.

If it does need to be efficient, you should look into algorithms like the Sieve of Eratosthenes.

If it does need to be efficient, you should look into algorithms like the Sieve of Eratosthenes.

Posted 18 October 2012 - 04:38 AM

I'm not really looking for it to be efficient, but I'm wondering if I can tweak this code so that it only prints the numbers it finds to be prime, instead of perfect numbers.

Posted 18 October 2012 - 09:48 AM

Okay, so I'm starting to get it. This is what I have so far:

Everything seems to work just how I want, but the only problem is that I keep getting the next number above my set limit (k).

For example, if I input 50. I get every prime number up to 50, but I also get 53.

If I input 100, I get every prime number up to 100, but I also get 101.

Does anyone know how I can fix this?

k = int (input ('Enter an integer...')) n = 2 while n <= k: t = 2 while t < n: if n % t == 0: n = n + 1 t = 2 else: t = t + 1 print (n) n = n + 1

Everything seems to work just how I want, but the only problem is that I keep getting the next number above my set limit (k).

For example, if I input 50. I get every prime number up to 50, but I also get 53.

If I input 100, I get every prime number up to 100, but I also get 101.

Does anyone know how I can fix this?

Posted 18 October 2012 - 10:36 AM

The reason this happens is that the inner loop increments n as well, but doesn't check whether n stays smaller than or equal to k. So one fix would be to add the n <= k condition to the inner loop too.

However a better solution would be to move the prime checking code into its own function, which would simply return False when finding a divisor instead of continuing with the next n. That would definitely simplify the logic.

However a better solution would be to move the prime checking code into its own function, which would simply return False when finding a divisor instead of continuing with the next n. That would definitely simplify the logic.

Page 1 of 1

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- ASP.NET
- .NET Framework
- VB6
- PHP
- Mobile Development
- Python
- Ruby
- Game Development
- Databases
- ColdFusion
- Assembly
- Other Languages
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- List Comprehensions An Introduction
- Python String Formatting
- Tkinter Overview, With a Fixed-Width Grid
- Python with Timmy the Turtle 2
- Python with Timmy the Turtle (and Friends)
- Creating, Renaming and Deleting Directories using the OS module
- timeit module
- Advanced Python Generators
- Calculating the runtime of a function
- A Basic PyQt Tutorial (Notepad)
**39 More Python Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- ASP.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- ASP Snippets
- Linux, Unix, and Bash Snippets
- Other Languages Snippets
- Regex

Copyright 2001-2016 **MediaGroup1 LLC**, All Rights Reserved

A**MediaGroup1 LLC** Production - Version 6.0.2.1.36

Server: secure3

A

Server: secure3