Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit baac400

Browse files
Merge pull request fnplus#339 from the-code-meister/polite_num
Polite numbers in c++,python,java
2 parents 430eff3 + 881ec19 commit baac400

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#program to find politeness of number n
2+
#include <iostream>
3+
using namespace std;
4+
5+
// A function to count all odd prime factors
6+
// of a given number n
7+
int countOddPrimeFactors(int n)
8+
{
9+
int result = 1;
10+
11+
// Eliminate all even prime factor of number of n
12+
while (n % 2 == 0)
13+
n /= 2;
14+
15+
// n must be odd at this point, so iterate for only
16+
// odd numbers till sqrt(n)
17+
for (int i = 3; i * i <= n; i += 2) {
18+
int divCount = 0;
19+
20+
// if i divides n, then start counting of
21+
// Odd divisors
22+
while (n % i == 0) {
23+
n /= i;
24+
++divCount;
25+
}
26+
27+
result *= divCount + 1;
28+
}
29+
30+
// If n odd prime still remains then count it
31+
if (n > 2)
32+
result *= 2;
33+
34+
return result;
35+
}
36+
37+
int politness(int n)
38+
{
39+
return countOddPrimeFactors(n) - 1;
40+
}
41+
42+
// Driver program to test above function
43+
int main()
44+
{
45+
int n = 90;
46+
cout << "Politness of " << n << " = "
47+
<< politness(n) << "\n";
48+
49+
n = 15;
50+
cout << "Politness of " << n << " = "
51+
<< politness(n) << "\n";
52+
return 0;
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Java program to find politeness of a number
2+
3+
public class Politeness {
4+
// A function to count all odd prime factors
5+
// of a given number n
6+
static int countOddPrimeFactors(int n)
7+
{
8+
int result = 1;
9+
10+
// Eliminate all even prime factor of number of n
11+
while (n % 2 == 0)
12+
n /= 2;
13+
14+
// n must be odd at this point, so iterate
15+
// for only odd numbers till sqrt(n)
16+
for (int i = 3; i * i <= n; i += 2) {
17+
int divCount = 0;
18+
19+
// if i divides n, then start counting of
20+
// Odd divisors
21+
while (n % i == 0) {
22+
n /= i;
23+
++divCount;
24+
}
25+
26+
result *= divCount + 1;
27+
}
28+
// If n odd prime still remains then count it
29+
if (n > 2)
30+
result *= 2;
31+
32+
return result;
33+
}
34+
35+
static int politness(int n)
36+
{
37+
return countOddPrimeFactors(n) - 1;
38+
}
39+
40+
public static void main(String[] args)
41+
{
42+
int n = 90;
43+
System.out.println("Politness of " + n + " = "
44+
+ politness(n));
45+
46+
n = 15;
47+
System.out.println("Politness of " + n + " = "
48+
+ politness(n));
49+
}
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Python program to find politeness of number
2+
3+
# A function to count all odd prime factors
4+
# of a given number n
5+
def countOddPrimeFactors(n) :
6+
result = 1;
7+
8+
# Eliminate all even prime factor of
9+
# number of n
10+
while (n % 2 == 0) :
11+
n /= 2
12+
13+
# n must be odd at this point, so iterate
14+
# for only odd numbers till sqrt(n)
15+
i = 3
16+
while i * i <= n :
17+
divCount = 0
18+
19+
# if i divides n, then start counting
20+
# of Odd divisors
21+
while (n % i == 0) :
22+
n /= i
23+
divCount = divCount + 1
24+
25+
result = result * divCount + 1
26+
i = i + 2
27+
28+
# If n odd prime still remains then count it
29+
if (n > 2) :
30+
result = result * 2
31+
32+
return result
33+
34+
35+
def politness( n) :
36+
return countOddPrimeFactors(n) - 1;
37+
# Driver program to test above function
38+
n = 90
39+
print "Politness of ", n, " = ", politness(n)
40+
n = 15
41+
print "Politness of ", n, " = ", politness(n)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /