30) Write a program to display all of the integers from 1 up to and including some integer entered by the
user followed by a list of each number's prime factors. Numbers greater than 1 that only have a single
is 100 print that the temperature is at the boiling
If it is above 100, print that the temperature is above the boiling point.
prime factor will be marked as prime.
Answers
Answer:
def primeFactors(n):
# Print the number of two's that divide n
while n % 2 == 0:
count.append(2)
n = n / 2
# n must be odd at this point
# so a skip of 2 ( i = i + 2) can be used
for i in range(3,int(math.sqrt(n))+1,2):
# while i divides n , print i and divide n
while n % i== 0:
count.append(int(i))
n = int(n / i)
# Condition if n is a prime
# number greater than 2
if n > 2:
count.append(int(n))
import math
n=int(input("Enter the maximum value to display:"))
count=[]
print("1 = 1")
for j in range(2,n+1):
primeFactors(j)
if len(count)==1:
print(j,"=",count[0],"(prime)",end="")
else:
print(j,"=",count[0],end="")
if len(count)==1:
print()
for i in range(1,len(count)):
print("x{}".format(count[i]),end="")
print()
count.clear()
Explanation:
keep learnning!!!
Program in C++:
#include<iostream>
#include<math.h>
using namespace std;
void primeFactors(int n)
{
while (n % 2 == 0)
{
cout<<2<<" * ";
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i + 2)
{
while (n % i == 0)
{
cout<<i<<" * ";
n = n/i;
}
}
if (n > 2)
{
cout<<n<<" * ";
}
}
int main()
{
int n;
cout<<"Enter an integer : ";
cin>>n;
for(int i = 1; i <= n; i++)
{
if(i == 1)
{
cout<<"1 = 1"<<endl;
}
else
{
int flag = 0;
for(int j = 2; j <= i/2; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
cout<<i<<" = "<<i<<" (prime)"<<endl;
}
else
{
cout<<i<<" = ";
primeFactors(i);
cout<<endl;
}
}
}
return 0;
}
Output:
Enter an integer : 5
1 = 1
2 = 2 (prime)
3 = 3 (prime)
4 = 2 * 2 *
5 = 5 (prime)