Epic Systems Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 vote

The loop can be optimized:
for(int i = 1; i <= n/2; i++)

- AnupamC May 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The number is nothing but a perfect number. Here is the java code to find the perfect number:

public void isPerfectNumber(int n)
{
int sum = 0;

for(int i = 1; i <= n-1; i++)
{
if(n%i == 0)
{
sum = sum + i;
}
}
if(sum == n)
System.out.println("The number is a perfect number");
else
System.out.println("The number is not a perfect number");
}

- maggie9in May 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

invalid! any factor should not count 1. for example: 4 is the perfect number.

- Anonymous May 04, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

6 is perfect number , 4 is not

- Anonymous May 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes 6 is a perfect no. bcoz ( 1+2+3=6 )
next perfect no is 28 bcoz ( 1+2+4+7+14=28)

4 is not a perfect no

- Karen July 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python code:
def sumOfFactor(N):
oN = N
F = 2
SUM = 0;
while F < oN :
if N % F == 0:
SUM += F
N = N / F
#print(N)
else:
F+=1
return SUM == oN

for i in range(10000): #test 0 - 10000
if sumOfFactor(i):
print i

seems only 4 is satisfied?

- aoran May 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPerfect(int n)
{

  int sum=0;
  int m=n;		//save the original value
  
  int i=2;
  while (i<n)
  {
     if ( 0 == n%i)
     { 
        sum += i;
        n = n/i         // keep on another factor
        i=2;		//restart from 2
     }
     i++;
  }

 if (m!=n))sum += n;   //count the last factor

 return (sum == m);
}

- john May 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer written by maggie is correct

- Raj May 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about following solution?

Idea is to take scan from 2 to sqrt(n) and add i and n/i if n%i == 0;
eg.


for n = 12,
sqrt(n) ~ 3
sum = 1 + (2 + 6) + (3 + 4)

bool isPerfectNumber(int n)
{
int sum = 0;
int mid = sqrt(n);

if(mid*mid == n) sum+=mid;
if(n>0) sum+=1;

for(int i = 2 ; i < mid;i++)
{
if(n%i == 0) sum += i + n/i;
}

return sum == n;
}

- neo June 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity is o(sqrt(n))

- neo1234 June 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is a big difference between Big-Oh and Small-Oh.

Did you mean to say O(sqrt(n)) and not o(sqrt(n))? If so, I suggest you do some reading. If not, I suggest you do some reading.

- LOLer June 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yaa. i meant first. sorry for the typo.
i expected a comment on the algo :P

- neo1234 June 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class General {

int add = 0;
static int originalValue = 100;
public static void main(String[] args) {
if(args[0] != null){
originalValue = Integer.parseInt(args[0]);
}

General gn = new General();
gn.findFactor(originalValue, 2);
}

public int findFactor(int number,int divisor){
int value = number%divisor;
int returnvalue = -1;
if(value == 0){
returnvalue = number/divisor;
System.out.println(divisor);
add = add + divisor;
if(returnvalue == 1){
if(divisor == originalValue){
System.out.println("No factors, prime number");
}
else{
if(add == originalValue){
System.out.println("You are lucky, factors add to original value");
}
else{
System.out.println("No, the factors do not add to original value");
}

}
}else{
findFactor(returnvalue,divisor);
}
}
else{
findFactor(number,divisor+1);
}
return returnvalue;
}
}

- timbaktu July 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//============================================================
//Program to check if a number is sum of its factors or not
#include <iostream>
using namespace std;

int sumofFactors(int n) {
    int i=2;
    int sum = 0;
    int original = n;
    while(n!=1) {
        if(n%i==0) {
            n = n/i;
            sum += i;
            i = 2;
        }
        else i++;
    }
    return (sum == original);
}

int main(int argc,char **argv) {
    int number;
    cout<<"Enter a number: ";
    cin>>number;
    cout<<sumofFactors(number)?"TRUE":"FALSE";
}
//============================================================

- Rock September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Rock,
Your solution doesnt consider all factors. Please check for number 12. You start with 12 ( divide by 2) -> 6 (divide by 2) -> 3 (Divide by 3) -> 1 Stop! You didnot consider 4 as the factor!

- jobHunter March 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is not a perfect square, perfect square is the one that has odd number of factors but sum of factors not equal to number for ex: 25 factors are 1, 5, sum = 6. but solution by maggie is perfect except that it is not a perfect square.

- anon May 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//java code
boolean checksum(int x){
if (x <= 1)
return false;
int y = 2;
int count = 0;
while (y * Y <= x){
if (x % y == 0)
count += x + (x/y);
x++;
}

return (count + 1 == x;
}

- css March 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey38712" class="run-this">// Perfect number
// Using trial Division technique

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int main(){
int n;
cin>>n;
vector<int> factors;
factors.push_back(1);
for(int i=2;i*i<=n;i++){
if(n%i==0){
factors.push_back(i);
factors.push_back(n/i);
}
}
int result=0;
for(int i=0;i<factors.size();i++){
result+=factors[i];
}
if(result==n)
cout<<"Perfect Number"<<endl;
else
cout<<"Not a perfect number"<<endl;
return 0;
}</pre>

- einstein010 April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is the "1" counted to the factors of one number?

- Superlby May 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey65333" class="run-this">public void sumFactor (int num) {

int sum = 0;
int max = (int) num/2;

for (int i = 1; i <= max; i++) {
if (num%i==0)
sum += i;
}
if (sum == num) {
System.out.println (num + " is the sum of its factors");
}
}

</pre><pre title="CodeMonkey65333" input="yes">
</pre>

- Anonymous July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<iostream>
#include<math.h>

int main()
{
    int no=0,i=0;
    printf("Enter number\n");
    scanf("%d",&no);
    int sum=1;
    int psuedoNo=no;
    for(i=2;i<=sqrt(no);i++)
    {
                            if(no%i==0)
                            {
                                             sum=sum+i+no/i;
                            }
                            
    }              
        
        if(sum==no)
        printf("PErfect number");
        else
        printf("Not PErfect number");
        system("PAUSE");
}

Complexity O(sqrt(n))

- Anonymous October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{ int i,j,n,tmp=0,a[30]={0},sum=0;
printf("Enter the number upto which you want output\n");
scanf("%d",&n);
for(i=2; i<=n ; i++)
{ tmp=0;
sum=0;
for(j=1; j<=i/2+1; j++)
{ if(i%j==0)
{ a[tmp]=j;
tmp++;
sum=sum+j;
}

}
if(sum==i)
printf("%d, ",sum);
}
}

- @123 August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class factor {

	public static void main(String args[]) {
		int number = 496;
		int sum = 0;
		int i = 1;
		while (i <= number / 2) {
			if (number % i == 0) {
				sum = sum + i;
			}
			i++;
		}
		if (number == sum)
			System.out.println("perfect");
		else
			System.out.println("not perfect");
	}
}

- Sanjeev March 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isPerfectNumber(int n)
	{
		int i=0, sum=1;
		for(i=2;i*i<n;i++)
		{
				if(n%i==0)
				{
					sum+=i;
					sum += (n/i);
				}
		}
		if(i*i==n)
			sum+=i;
		return (sum==n);
	}

- coder14 December 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class sumFactorNumber {

		public static void main(String[] args){
			
			for(int n = 2; n<=100; n++)
			{
			int sum = 1; //1 is the factor of all numbers  
			
			
			for(int i=2; i<n; i++)
			{
				if(n%i==0)
				{
					sum = sum + i;
				}
			}
			
			
			if(sum==n)
				System.out.println(n+"is sum factor");
			}
		}
}

- nahidcse05 June 05, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More