r/dailyprogrammer • u/nint22 1 2 • Aug 06 '13
[08/06/13] Challenge #134 [Easy] N-Divisible Digits
(Easy): N-Divisible Digits
Write a program that takes two integers, N and M, and find the largest integer composed of N-digits that is evenly divisible by M. N will always be 1 or greater, with M being 2 or greater. Note that some combinations of N and M will not have a solution.
Example: if you are given an N of 3 and M of 2, the largest integer with 3-digits is 999, but the largest 3-digit number that is evenly divisible by 2 is 998, since 998 Modulo 2 is 0. Another example is where N is 2 and M is 101. Since the largest 2-digit integer is 99, and no integers between 1 and 99 are divisible by 101, there is no solution.
Author: nint22. Note: Sorry for the absence of challenges; I've been away for the last two weeks, and am getting back into the grove of things.
Formal Inputs & Outputs
Input Description
You will be given two integers, N and M, on standard console input. They will be space delimited values where N will range from 1 to 9, and M will range from 2 to 999,999,999.
Output Description
Print the largest integer within the range of 1 to the largest integer formed by N-digits, that is evenly-divisible by the integer M. You only need to print the largest integer, not the set of evenly-divisible integers. If there is no solution, print "No solution found".
Sample Inputs & Outputs
Sample Input 1
3 2Sample Output 1
998Sample Input 2
7 4241275Sample Output 2
8482550
21
u/llasarus 1 0 Aug 07 '13 edited Aug 08 '13
Brainfuck:
It's limited to 8bit numbers (n can no be larger than 2). I've tested it in my brainfuck interpreter with 32bit cells but it can't handle n > 3 without output problems. I could rewrite the print algorithm but I've already spent a few hours on this. The printing algorithm is surrounded by brackets and the value to be outputted is located 2 cells behind the pointer at the start of the algorithm.
Because of time limitation I haven't added error checking, but it would be possible by checking that the result is larger than 10n-1 and smaller than 10n (because of cell wrapping). But that is easier said than done.
The input must be formatted like this: [n][space][m][enter]. I have no idea what horrible things can go wrong if you don't do this correctly.
Edit: something went wrong with the formatting
Edit2: I forgot to readd the brackets signifying the print algorithm after i fixed the formatting
2
u/Taunk Aug 12 '13
I know I'm too late and all, but I wanted to solve this one anyway. First time poster: turns out number theory was useful after all.
Python:
29
u/douggums Aug 06 '13
Haskell:
Returns 0 if there are no solutions.
27
u/13467 1 1 Aug 07 '13
Valid Python, Ruby, and C99:
16
u/WildN00b Aug 09 '13
LOLCODE
My first LOLCODE program :)
15
u/Steve132 0 1 Aug 06 '13 edited Aug 07 '13
C++
Python
8
u/otsojaun Aug 06 '13
Java
Thank you always for these challenges! Really appreciate the work you put on this subreddit.
6
u/IForgetMyself Aug 07 '13 edited Aug 08 '13
My assembly (x64) versions, one version using remainders to find the largest value. And one without a division instruction.
On invalid input they return 0. Their prototypes are ulong f(ulong n, ulong m).
~
~
~
Also
and
Confused me a little, I assume we're counting leading zeros as digits?
edit: remove a useless line
6
u/mormon_still Aug 07 '13
would someone be able to provide some sort of proof/intuition as to why
provides the value we're looking for in this problem?
4
u/spacepotatoe Aug 07 '13
C - Haven't made many submissions here, so any comments are more than welcome :)
4
u/level1kid Aug 09 '13
Nothing special...
Python:
3
u/Cosmologicon 2 3 Aug 06 '13 edited Aug 07 '13
bc (pretty much the same as some other solutions here):
EDIT: and if you want to read
nandmfrom the input:3
u/Edward_H Aug 07 '13
My solution in COBOL, sadly not as simple as the other solutions:
3
u/bytbox Aug 07 '13
Go. Same way as every one else except with some casting to get around static typing.
3
u/h3ckf1r3 Aug 07 '13
Here's my ruby solution, maybe not the fastest, but I love it when my code fits in one line :)
It's pretty late so I may have made a typing error, but feel free to critique my code :).
3
u/JoTheKhan Aug 08 '13
First time on this sub, hope I submitted correctly Java
https://gist.github.com/anonymous/7563b647122d35e3e2af
3
u/Crauwolf Aug 08 '13
C#:
Fairly new to programming so any suggestions would be appreciated.
3
u/winged_scapula Aug 09 '13
Python, not very clever but..
5
u/5hassay Aug 06 '13
python33
2
u/thisisnotmypenis Aug 06 '13
Here's my solution in C, at first I didn't want to just use a string so I made this first awful function, the second one is way simpler...
2
u/475c Aug 06 '13
here's my C solution, command line
2
u/skyangelisme 0 1 Aug 06 '13
Fast solution in Clojure.
Input:
Output:
2
u/deds_the_scrub Aug 07 '13
Written in C, 2 ways, naive and smart:
2
u/MartiPanda Aug 07 '13
C# (.NET)
2
u/Xavdidtheshadow 0 0 Aug 07 '13
Ruby
2
u/serphz Aug 07 '13
C++
2
u/[deleted] Aug 08 '13
In Go:
2
u/MismatchedParens Aug 08 '13
Python 2.7.2
An extremely brute force solution, but I might try something a little more elegant soon.
EDIT: And looking at what other people have done, I feel like such an idiot.
2
u/Glassfish 0 0 Aug 08 '13
Python3:
2
u/TheBucklessProphet Aug 08 '13 edited Aug 08 '13
Python 2.7:
Feedback would be greatly appreciated.
2
u/ittybittykittyloaf Aug 08 '13
C++:
2
u/Rellikolbaid 0 0 Aug 08 '13 edited Aug 08 '13
Python 2.7.5
Any criticism welcomed.
2
u/Chromogenicity Aug 09 '13
Beginner here, with a JavaScript solution:
2
u/PolarisDiB Aug 09 '13
Python 2.7:
This is the first time I'm sharing my own, and I've only done three of these challenges. I'm finding the variations fascinating.
2
u/squire_louseII Aug 09 '13
Python 2.7. Hats off to user pyrapponst.
2
u/godzab Aug 09 '13 edited Aug 09 '13
Did this in c++, this is the earliest I have ever done a challange:
It's been a while since I have used c++, so any advice would be greatly appreciated.
2
u/JerMenKoO 0 0 Aug 11 '13 edited Aug 11 '13
Python 3.x oneliner: (the list or the dict? ;))
2
u/[deleted] Aug 12 '13
Written in normal C code:
2
u/[deleted] Aug 13 '13
Probably a terrible solution, also i have no clue how to do math to find a digit of length N.
2
u/[deleted] Aug 13 '13
I just recently found this subreddit, and I'm trying to get back into java before the semester starts. I really appreciate what you're doing here.
java
2
u/rsamrat Aug 16 '13
Clojure:
2
u/killedbythegrue Aug 16 '13
An Erlang solution
4>
2
u/jnazario 2 0 Aug 18 '13 edited Aug 18 '13
python 2:
2
u/lukz 2 0 Aug 19 '13
Common Lisp
2
u/MaxK Sep 06 '13 edited May 14 '16
This comment has been overwritten by an open source script to protect this user's privacy. It was created to help protect users from doxing, stalking, and harassment.
If you would also like to protect yourself, add the Chrome extension TamperMonkey, or the Firefox extension GreaseMonkey and add this open source script.
Then simply click on your username on Reddit, go to the comments tab, scroll down as far as possibe (hint:use RES), and hit the new OVERWRITE button at the top.
2
u/iowa116 Sep 18 '13
Here's my python solution.
2
u/[deleted] Nov 05 '13
Java:
2
u/pirate_platypus Nov 05 '13
Python 2:
2
u/lets_see_exhibit_A Nov 06 '13
Java:
2
u/kate_katie_kat Nov 07 '13
Ruby:
2
u/Hanse00 Nov 12 '13
Python 2.7, fairly raw and simple, but gets the job done
2
u/jnazario 2 0 Dec 01 '13 edited Dec 02 '13
been learning scala some this past month after learning some F# earlier this year. got to play with toOption, Some and None for this one.
ran out of heap space for the second sample input, though. clearly a sign i'm doing it wrong.
EDIT got rid of the memory errors after some thought. my original solution used util.Try, toOption, Some and None but this new one doesn't.
2
u/davidschromebook Dec 07 '13 edited Dec 09 '13
javascript (runs in console) feedback please
2
u/VerifiedMyEmail Jan 08 '14 edited Jan 08 '14
python
2
u/jeaton Jan 09 '14
Python:
2
u/[deleted] Jan 24 '14
Old thread, but that's not stopping me from answering:
It's not the prettiest, it's certainly not the most efficient, but I'm still proud of it...
3
u/eruonna Aug 06 '13
Haskell
3
u/EvanHahn Aug 06 '13
Ruby:
2
u/Liiiink Aug 06 '13 edited Aug 06 '13
My go at PHP, should work on command line
2
u/[deleted] Aug 07 '13
Java - For some reason my last loop always produces a 'result' value of 0.
public class Challenge {
}
1
u/chunes 1 2 Aug 06 '13 edited Aug 06 '13
Java — uses string concatenation to arrive at the maximum n-digit integer because I am dumb in the maths.
1
u/na85 Aug 06 '13 edited Aug 06 '13
Fortran
1
u/toodim Aug 06 '13
Python
Slow Solution:
Faster Solution:
1
u/eBtDMoN2oXemz1iKB Aug 07 '13
Ruby
1
u/bl4k0ut Aug 07 '13
D
1
u/jkn Aug 07 '13
Solution in Javascript on node.js
(Really cool tricks on other comments!)
My initial version:
Adapted to the cool tricks:
1
u/DuckSwapper Aug 07 '13
An approach to make it as short as possible, Python:
1
u/stiicky 0 0 Aug 07 '13
Perl
pretty much the same as what everyone else posted.
1
u/13467 1 1 Aug 07 '13
J
1
u/yitz Aug 07 '13
Haskell
The above requires the following imports, either at the top of your module file or at the GHCi prompt:
1
u/aerozepplin Aug 07 '13
Programming Noob here. Here is my first attempt in python 2.7 .
1
u/koodz Aug 07 '13
Java
1
u/Alca_Pwn Aug 07 '13
perl
1
u/[deleted] Aug 07 '13
Ruby:
1
u/mormon_still Aug 07 '13
C++:
1
u/courtstreet Aug 07 '13
In python:
1
u/CatZeppelin Aug 07 '13 edited Aug 08 '13
Very similar to other designs, nonetheless, I think it's worth sharing.
No error checking due to time constraints. Would anyone be interested in an ARM assembly version? I've been learning a few mnemonics over the past week -- I really need a project to learn more.
EDIT: I shouldn't have added
Lto the constant -- constants are the biggesttypethat fits (ie:longon a 64 bit machine).1
u/Idra_rage_lulz Aug 07 '13 edited Aug 07 '13
C++11
1
u/[deleted] Aug 11 '13
My C++ solution.
1
u/Die-Nacht 0 0 Aug 11 '13
Haskell
1
u/Quiscalus Aug 20 '13
I just discovered this subreddit and was going to post my answer, but then I saw how fucking concise everyone else got it and I felt disgraced about my code lol. Such is life.
1
u/[deleted] Sep 06 '13
Python
1
u/Elias_The_Thief Oct 28 '13
Java Solution: import java.util.Scanner;
1
u/IceDane 0 0 Aug 06 '13
Haskell
1
u/jonathanwaltz Aug 06 '13
Java solution, feedback welcome
1
u/Nurchu Aug 06 '13
Java - Probably a bit over complicated
1
u/Coder_d00d 1 3 Aug 06 '13
C -- simple brute force
1
u/codemac Aug 06 '13 edited Aug 07 '13
Haskell !
EDIT: Wow these other answers are consise! The little
trick I deduced immediately, but the
part is awesome!
1
u/skeeto -9 8 Aug 06 '13
JavaScript using a closed-form solution,
1
u/[deleted] Aug 07 '13 edited Aug 07 '13
Java
1
u/spfy Aug 07 '13
Java - I'm trying to learn. I feel my getAnswer() method moves too far to the right, comparing it to all of you lol. I think it works okay, though!
1
u/okonom Aug 07 '13
Python
1
u/pahefu Aug 07 '13
Python
Trying to trick lamba with local variables. Returns the value only if result has N digits.
1
u/Hemse Aug 07 '13
Python 2.7
1
u/[deleted] Nov 17 '13 edited Oct 08 '19
deleted 0.8040 What is ^^^this?
0
u/[deleted] Aug 10 '13 edited Aug 10 '13
C++, feedback is appreciated.