Modifying program C to Python.
The c program I want to get first is :
#include <stdio.h>
void main()
{
int i, j, count = 1;
long sum = 0;
printf("Program \n") to obtain a decimal number between 1 and 1000);
printf("%d\t", 1);
for (i = 1; i <= 1000; i++) {
for (j = 2; j < i; j++) {
if ((i % j) == 0)
break;
}
if (i == j) {
printf("%d\t", i);
count++;
if ((count % 8) == 0)
printf("\n");
}
}
The number of decimals between printf("\n1 and 1000" is %d. \n", count);
}// Enter code here
The python program you just created is:
#!/usr/bin/python
count=1
number = input("what is number:")
for i in range(1,number+1):
for j in range (2,i):
if i%j == 0
break;
elif i==j:
count +=1
print i
if count%8 == 0
print(1\n )
#python prime.py
what is number:1000
[root@horyundangIII yoon~]#
[root@horyundangIII yoon~]#
No answer ^^;; what should I do?
python c c++
If you just want the answer... Have you heard of Erathosthenes sce?
# I just scooped and annotated the Wikipedia article that I linked above.
# It feels different when mathematicians code.
def prime_list(n):
# First, assume that all numbers are prime numbers.
sieve = [True] * n
# Find the natural number that can be the maximum number of n
m = int(n ** 0.5)
# 2, 3,... Based on the natural number,
for i in range(2, m+1):
# Examine all numbers assumed to be prime.
if sieve[i] == True:
# For example, if the natural number is 6, then all the numbers up to 6, 12, 18, ..., n are the numbers
for j in range(i+i, n, i):
# Reverses that it is not a prime number.
sieve[j] = False
# But if there are still numbers that are considered prime numbers, we return them.
return [i for i in range(2, n) if sieve[i] == True]
prime_list(20)
# [2, 3, 5, 7, 11, 13, 17, 19]
In my opinion, this task is not really a Python task, but rather a task for the sensation that most efficiently embodies the (mathematical) principles already given. It would be nice to explore the mathematical principles of finding prime numbers in depth first.
© 2025 OneMinuteCode. All rights reserved.