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 45ce78e

Browse files
Merge pull request fnplus#344 from AkashM398/master
simple_queue.py: Add Queue in Python
2 parents 7d7ab4f + f255799 commit 45ce78e

File tree

8 files changed

+105
-57
lines changed

8 files changed

+105
-57
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* C Program to Find Perfect Number in a given interval eg. 1 to 1000 */
2+
3+
#include <stdio.h>
4+
5+
int main()
6+
{
7+
int Number, Sum, i, Minimum, Maximum;
8+
9+
printf("\n Please Enter the Minimum & Maximum Values\n");
10+
scanf("%d %d", &Minimum, &Maximum);
11+
12+
printf("Perfect Numbers Between %d and %d are:\n", Minimum, Maximum);
13+
for(Number=Minimum; Number<=Maximum; Number++)
14+
{
15+
for(i = 1, Sum =0 ; i < Number ; i++)
16+
{
17+
if(Number % i == 0)
18+
Sum = Sum + i ;
19+
}
20+
21+
if (Sum == Number)
22+
printf(" %d ", Number) ;
23+
}
24+
25+
return 0;
26+
}

‎Algorithms/Maths/Perfect Number/C/readme.md‎

Whitespace-only changes.

‎Algorithms/Maths/Perfect Number/CPP/readme.md‎

Whitespace-only changes.
Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,37 @@
1-
import java.io.*;
2-
import java.util.*;
3-
4-
public class PerfectNumber {
1+
public class PerfectNumber {
52

6-
public static boolean isPalindrome(int n)
7-
{
8-
// if divisible by 11 then true
9-
if (n % 11 == 0)
10-
{
11-
return true;
12-
}
13-
14-
// if not divisible by 11
15-
return false;
3+
static boolean isPerfect(int n) {
4+
// the smallest perfect number is 6 [1]
5+
// [1] https://oeis.org/A000396
6+
if (n < 6) {
7+
return false;
8+
}
9+
10+
// this is just an optimization, since it's proved [2] that any possible
11+
// odd perfect number must be greater than 10^1500, which is greater
12+
// than the int size
13+
// [2] http://www.lirmm.fr/~ochem/opn/opn.pdf
14+
if (n % 2 != 0) {
15+
return false;
16+
}
17+
18+
// the algorithm itself
19+
int sqrtOfN = (int) Math.sqrt(n);
20+
int aliquotSum = 1;
21+
for (int i = 2; i <= sqrtOfN; i++) {
22+
if (n % i == 0) {
23+
aliquotSum += i + n/i;
24+
}
25+
}
26+
27+
return aliquotSum == n;
28+
}
29+
30+
public static void main(String[] args) {
31+
int[] testCases = {-6, 0, 6, 7, 25, 28, 496, 8128, 33550336};
32+
33+
for (int i : testCases) {
34+
System.out.println(String.format("Is %d a perfect number? %s", i, isPerfect(i)));
35+
}
1636
}
17-
public static void main(String args[]) {
18-
int i,n,j,flag,number;
19-
Scanner in = new Scanner(System. in);
20-
System.out.println("Input number ");
21-
i = in.nextInt();
22-
23-
24-
for(n=10;n<=i;n++)
25-
{
26-
int l=Integer.toString(n).length();
27-
if(l%2==0){
28-
flag=1;
29-
number=n;
30-
while(number>0)
31-
{
32-
33-
int digit = number % 10;
34-
if(!(digit==4 || digit==5))
35-
{
36-
flag=0;
37-
}
38-
number=number/10;
39-
40-
}
41-
if(flag==1)
42-
{
43-
44-
45-
boolean num=isPalindrome(n);
46-
47-
if(num)
48-
{
49-
System.out.println(n);
50-
}
51-
}
52-
}
53-
54-
55-
}
56-
57-
58-
}
59-
}
37+
}

‎Algorithms/Maths/Perfect Number/Java/readme.md‎

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python Program to find Perfect Number using For loop
2+
3+
Number = int(input(" Please Enter any Number: "))
4+
Sum = 0
5+
for i in range(1, Number):
6+
if(Number % i == 0):
7+
Sum = Sum + i
8+
if (Sum == Number):
9+
print(" %d is a Perfect Number" %Number)
10+
else:
11+
print(" %d is not a Perfect Number" %Number)

‎Algorithms/Maths/Perfect Number/Python/readme.md‎

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Python Program for Queue Data Structure implementation
2+
3+
class Queue:
4+
def __init__(self):
5+
self.items = []
6+
7+
def is_empty(self):
8+
return self.items == []
9+
10+
def enqueue(self, data):
11+
self.items.append(data)
12+
13+
def dequeue(self):
14+
return self.items.pop(0)
15+
16+
17+
q = Queue()
18+
while True:
19+
print('enqueue <value>')
20+
print('dequeue')
21+
print('quit')
22+
do = input('What would you like to do? ').split()
23+
24+
operation = do[0].strip().lower()
25+
if operation == 'enqueue':
26+
q.enqueue(int(do[1]))
27+
elif operation == 'dequeue':
28+
if q.is_empty():
29+
print('Queue is empty.')
30+
else:
31+
print('Dequeued value: ', q.dequeue())
32+
elif operation == 'quit':
33+
break

0 commit comments

Comments
(0)

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