Calculate the Sum Seed of a number




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

#!/usr/bin/perl/
my $number;
my $total_digits=0;
my $temp_num;
my @digits;
my $counter;
my $temp_number;
my $flag=0;
$number = shift;
$temp_num=$number;
@digits= &split_digits ($number);
$total_digits=@digits;
$temp_num=$number-(9*$total_digits);
for ($count=$temp_num;$count < $number ;$count++) {
@digits=undef;
@digits=&split_digits ($count);
$temp_number=&digit_sum();
$temp_number=$temp_number+$count;
if($temp_number==$number)
{
print "$number = $count+ @digits \n";
$flag=1;
}
}
if($flag==0)
{
print "Sorry!! no sum seed for number $number";
}

sub split_digits()
{
my $number=shift;
my @digit;
@digit=split (undef,$number);
return(@digit);
}

sub digit_sum()
{ my $sum=0;
my $counter;
for ($counter=0;$counter < $total_digits ;$counter++) {
$sum=$sum+$digits[$counter];
}
return($sum);

}

- H.M October 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain what you're doing here? (In pseudocode or the algorithm)

- sawrubh October 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hi,

Our question here was to generate all the numbers whose sum of digits when added to that number results to input number. So to do this we can do following

a) Take the input number
b) find total digits in that number (i.e if input number is 256 total of digit is 3).
c) Subtract 9 for each digit in the number (i.e if number is 256 substract 3*9 = 27 from 256. so new number will be 256-27= 229).
d) now check all numbers from new number (229) to input number (256) ie if sum of digits added to number gives input number.
e) if any number found print that number (245 which is between 229 & 256, when added to 2+4+5 gives 256)
f) if any number is not found print error message (if input number is 108, no combination is found)

- mathur1987 October 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/perl/
my $number;my $total_digits=0;my $temp_num;my @digits;my $counter;my $temp_number;my $flag=0;$number = shift;$temp_num=$number;@digits= &split_digits ($number);$total_digits=@digits;$temp_num=$number-(9*$total_digits);for($count=$temp_num;$count < $number ;$count++){@digits=undef;@digits=&split_digits $count);$temp_number=&digit_sum();$temp_number=$temp_number+$count;if($temp_number==$number){print "$number = $count+ @digits \n";$flag=1;}}if($flag==0){print "Sorry!! no sum seed for number $number";}sub split_digits(){my $number=shift;my @digit;@digit=split (undef,$number);return(@digit);}sub digit_sum(){my $sum=0;my $counter;for ($counter=0;$counter < $total_digits ;$counter++) {$sum=$sum+$digits[$counter];}return($sum);}

- Harshit Mathur October 20, 2014 | 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