We're a place where coders share, stay up-to-date and grow their careers.
Perl solution:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::AllUtils qw{ min_by }; my @nums = (100, 200, 400, 800, 1600, 3200, 6400, 128000); my $given_num = 900; say $nums[ min_by { abs($nums[$_] - $given_num) } 0 .. $#nums ];
Perl solution: