Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10 harshad number #467

Merged
merged 4 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions 100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-module(doors_100).
-compile(export_all).
-import(lists).

main() ->
%% false represents a closed door & true represents an open door
Doors = lists:duplicate(100, false),
Result = toggle_doors( Doors, 0 ),
io:format("Open doors:~n"),
print_open_doors( Result, 1),
OpenDoorsIndex = [ X*X || X <- lists:seq( 1, 10) ],
TestPassed = test_open_doors( Result, 1, OpenDoorsIndex ),
if TestPassed == true ->
io:format("~nTest passed~n");
true ->
io:format("~nTest failed~n")
end.

test_open_doors(_, 101, _) ->
true;

test_open_doors(Result, CurrentIndex, OpenDoorsIndex) ->
IsAnOpenDoor = lists:member( CurrentIndex, OpenDoorsIndex ),
%% index for nth should be >=1
Door = lists:nth( CurrentIndex, Result ),
if IsAnOpenDoor == true ->
if Door == true ->
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
true ->
io:format("test failed for ~p~n", [CurrentIndex]),
false
end;
true ->
if Door == false ->
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
true ->
io:format("test failed for ~s~n", [CurrentIndex]),
false
end
end.

print_list([], _) ->
io:format("~n");
print_list([H|T], N) ->
io:format("~p ", [H]),
if (N rem 10) == 0 ->
io:format("~n"),
print_list(T, N+1);
true ->
print_list(T, N+1)
end.

print_open_doors([], _) ->
io:format("~n");
print_open_doors([H|T], N) ->
if H == true ->
io:format("~p ",[N]),
print_open_doors(T, N+1);
true ->
print_open_doors(T, N+1)
end.

toggle_doors( Doors, 100 ) -> Doors;
toggle_doors( Doors, Turn) ->
toggle_doors( toggle_doors_per_turn( Doors, 1, Turn), Turn + 1 ).

toggle_doors_per_turn( Doors, Current, Turn ) ->
if Current*(Turn+1) > 100 ->
Doors;
true ->
ToggledDoors = lists:sublist( Doors, Current*(Turn + 1) - 1 ) ++
[ not lists:nth( Current*(Turn + 1) , Doors) ] ++
lists:sublist( Doors, Current*(Turn + 1) + 1, length( Doors ) - Current*(Turn + 1) + 1 ),
toggle_doors_per_turn( ToggledDoors, Current + 1, Turn )
end.
52 changes: 52 additions & 0 deletions 100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env perl

use strict;
use warnings;

use POSIX;

sub toggle_doors {
my ($doors, $start) = @_;
for( my $i = 1; ($start*$i - 1) < 100; $i++ ) {
$doors->[ $start*$i - 1 ] = !$doors->[ $start*$i - 1 ];
}
}

sub print_open_doors {
my ($doors) = @_;
print "Open doors:\n";
for( my $i = 0; $i < 100; $i++ ) {
if( $doors->[ $i ] ) {
print ($i + 1);
print " ";
}
}
print "\n";
}

sub test_doors {
my $doors = shift;

my @expected = (0) x 100 ;

for( my $i = 1; $i <= 10; $i++ ) {
$expected[ ($i*$i) - 1 ] = 1;
}

for( my $i = 0; $i <= $#expected; $i++ ) {
if( $doors->[ $i ] != $expected[ $i ] ) {
die "test failed for index $i\n";
}
}

print "\nAll tests passed\n\n";
}

my @doors = ( 0 ) x 100;
for( my $i = 1; $i <= 100; $i++ ) {
toggle_doors( \@doors, $i );
}

print_open_doors( \@doors );
test_doors( \@doors );

37 changes: 37 additions & 0 deletions 10_Harshad_Number/Erlang/Sweet-kid/harshad_number.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-module(harshad_number).
-compile(export_all).

main() ->
Numbers = lists:seq(1, 200),
HarshadNumbers = lists:filter(
fun(Number) ->
Digits = integer_to_list(Number),
Sum = sum( Digits, 0 ),
if Number rem Sum == 0 ->
true;
true ->
false
end
%% end if
end
%% end fun(Number)
, Numbers),
Expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
12, 18, 20, 21, 24, 27, 30, 36,
40, 42, 45, 48, 50, 54, 60, 63,
70, 72, 80, 81, 84, 90, 100, 102,
108, 110, 111, 112, 114, 117, 120,
126, 132, 133, 135, 140, 144, 150,
152,153, 156, 162, 171, 180, 190,
192, 195, 198, 200],
if HarshadNumbers == Expected ->
io:format("Test passed~n");
true ->
io:format("Test failed~n")
end.

sum([], Sum) ->
Sum;
sum([H|T], Sum) ->
{Integer,_} = string:to_integer([H]),
sum(T, Sum + Integer).
30 changes: 30 additions & 0 deletions 10_Harshad_Number/Perl/Sweet-kid/10_Harshad_Number.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use HarshadNumber qw(is_harshad_number);

my $expected = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
12, 18, 20, 21, 24, 27, 30, 36,
40, 42, 45, 48, 50, 54, 60, 63,
70, 72, 80, 81, 84, 90, 100, 102,
108, 110, 111, 112, 114, 117, 120,
126, 132, 133, 135, 140, 144, 150,
152,153, 156, 162, 171, 180, 190,
192, 195, 198, 200
];

my @result;
for my $i( 1 .. 200 ) {
if( HarshadNumber::is_harshad_number( $i ) ) {
push @result, $i;
}
}

is_deeply(\@result, $expected, "Harshad numbers test passed");

done_testing();
30 changes: 30 additions & 0 deletions 10_Harshad_Number/Perl/Sweet-kid/10_Harshad_Number.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use HarshadNumber qw(is_harshad_number);

my $expected = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
12, 18, 20, 21, 24, 27, 30, 36,
40, 42, 45, 48, 50, 54, 60, 63,
70, 72, 80, 81, 84, 90, 100, 102,
108, 110, 111, 112, 114, 117, 120,
126, 132, 133, 135, 140, 144, 150,
152,153, 156, 162, 171, 180, 190,
192, 195, 198, 200
];

my @result;
for my $i( 1 .. 200 ) {
if( HarshadNumber::is_harshad_number( $i ) ) {
push @result, $i;
}
}

is_deeply(\@result, $expected, "Harshad numbers test passed");

done_testing();