Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1602
Members Online:
2
Guests Online:
1

Total Post History
Posts:
80573
Topics:
18459

7-Day Post History
New Posts:
4
New Topics:
2
Active Topics:
4

Our newest member
huezang

Other

FAQ
All times are UTC [ DST ]

Google Ads

LTD Puzzle 19: Take numbers between 1 and 100

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

LTD Puzzle 19: Take numbers between 1 and 100

Postby SQLDenis on Fri Jan 23, 2009 6:41 pm

Take numbers between 1 and 100 and do the following

    if the number is a prime mark it
    if the number is divisible by 5 mark it
    if the number is divisible by 7 mark it
    if the number is divisible by 7 and 5 mark it

result would be something like this

numberPrimediv by 5div by 7div by 5 and 7
1nononono
2yesnonono
..........
..........
..........
35noyesyesyes
..........
..........
..........
100noyesnono


you can choose any language you want
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
 
Posts: 21760
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: Take numbers between 1 and 100 and do the following

Postby SQLDenis on Fri Jan 23, 2009 7:26 pm

Here is my solution in Transact SQL
Code is hidden, SHOW
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
 
Posts: 21760
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 4:05 pm

Here's the fastest answer i can give in VB .Net.
Would normally complete puzzles in C#, but i felt like doing one in VB .Net as it is my first language, and i can't neglect the poor thing :(

Code is hidden, SHOW


This produced the following result on my computer.
(which looks much sexier in the console window! :P)
Seems I underestimated the forum, which does print the output in a lovely system font.
nice one! =D
Code is hidden, SHOW
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 4:11 pm

Rizzo,

Check your results again. 5 is divisible 5, right?
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 4:36 pm

d'oh! Well spotted gmmastros. Here's the edit:

Code is hidden, SHOW


And the new result. I lost 10 milliseconds! But at least it's not completely wrong now! :D

Code is hidden, SHOW

:blush:
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 4:58 pm

My version, in vb6:

To run this code, create a project with a single form. There should be a button and a textbox (named txtOutput) configured for multi-line.

Code is hidden, SHOW


With values 1 to 100, this runs so fast that it's claiming 0 milliseconds. If I change this to 1 to 10,000, it runs in 62 milli-seconds on my computer.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 5:08 pm

Ah, very nice gmmastros ^^
I think i should remove my < tests. They're just creating more work than they're saving now that it's only < 5 lol.

I'll probably repost this one in a different language later.
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 5:15 pm

Rizzo,

One of the reasons why I enjoy these puzzles is because I don't have an opportunity to have my code reviewed by my peers. I bet there are a lot of programmers in a similar situation. I've created a similar solution in vb2008, and it executes even faster. With numbers from 1 to 10,000 it executes in 12 milliseconds (4 or 5 times faster than the VB6 version). If you'd like, I'd be willing to share my thoughts with you on how you can speed up your code.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 5:24 pm

Constructive criticism is always welcome :D
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 5:39 pm

Ok.

I'll put my comments in the hide box so it won't affect the code that others may write.

Code is hidden, SHOW
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 5:52 pm

Thanks for the feedback.

I used a large string because i found that the Console.WriteLine was taking the bulk of time to execute and a single large call took much less time than multiple small ones. I'll try it with an array definately.

With the db5 and db7 booleans, i figured that if i set these to true or false, i can simply test the boolean values when it comes to building the string. This means i only do two modulus calculation, rather than four (/5, /7, /5 & /7).
Is there no performance penalty to performing the modulus twice more compared to storing the result of the modulus as booleans and then testing the booleans?

Thanks again for the feedback ^_^
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 6:01 pm

rizzo wrote:Thanks for the feedback.
With the db5 and db7 booleans, i figured that if i set these to true or false, i can simply test the boolean values when it comes to building the string. This means i only do two modulus calculation, rather than four (/5, /7, /5 & /7).
Is there no performance penalty to performing the modulus twice more compared to storing the result of the modulus as booleans and then testing the booleans?


I don't know. You'd have to test that. I do know that if a number is divisible by 5 and 7, it's also divisible by 35, so you can just test for that. You'd probably have to increase the loop to 1,000,000 to test the timing of it though. Mod division is very fast to begin with.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby rizzo on Sat Jan 24, 2009 6:50 pm

Thanks for the advice gmmastros. Got it running at 0ms on 100 entries.
It spiradically takes 10ms or 0ms to do 1000 but i think that's processor task switching or it would always take 10ms.

The new code is:
Code is hidden, SHOW


And the new output is:
Code is hidden, SHOW


Thanks for the advice gmmastros, you're a legend. :thumright:
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Sat Jan 24, 2009 6:58 pm

Well done. :)
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby chrissie1 on Sat Jan 24, 2009 7:01 pm

My first try in C#

Code is hidden, SHOW


45 ms on my system.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby chrissie1 on Sat Jan 24, 2009 7:50 pm

uhm this is weird. I couldn't get it any faster in a console app. So George tried as a windows app and there nearly the same code runs in 0 ms.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby SQLDenis on Sat Jan 24, 2009 7:55 pm

Chrissie,

In that case a WPF app should be the fastest :-)
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
LTD Gold - Rating: 3455LTD Gold - Rating: 3455LTD Gold - Rating: 3455
 
Posts: 21760
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby chrissie1 on Sat Jan 24, 2009 8:00 pm

Well the solution was simple as dirt.

Code is hidden, SHOW


8 ms for 10000 elements on my machine.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby HarleyQuinn on Mon Jan 26, 2009 2:49 pm

VB6 version I wrote when Denis first suggested this puzzle (I've modifed the timing to be consistent with George's version). Runs at almost exactly same speeds as George's as well.

Code is hidden, SHOW


edited by chrissie1: used hidecode tags

Cheers for the edit Chrissie ;)
Andy
User avatar
HarleyQuinn
LTD Admin
LTD Admin
LTD Bronze - Rating: 46
 
Posts: 308
Joined: Tue Dec 18, 2007 3:14 pm
Location: Sheffield, UK

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby HarleyQuinn on Mon Jan 26, 2009 3:36 pm

Grr, and my C# implementation of it was 3ms slower than Chrissie's :thumbdown:

;)
Andy
User avatar
HarleyQuinn
LTD Admin
LTD Admin
LTD Bronze - Rating: 46
 
Posts: 308
Joined: Tue Dec 18, 2007 3:14 pm
Location: Sheffield, UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby chrissie1 on Mon Jan 26, 2009 3:49 pm

That's why I wrote that song for Tina Turner.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby HarleyQuinn on Mon Jan 26, 2009 3:58 pm

:lol:

I was going for it being because I've only been writing C# for a week and a half ;)
Andy
User avatar
HarleyQuinn
LTD Admin
LTD Admin
LTD Bronze - Rating: 46
 
Posts: 308
Joined: Tue Dec 18, 2007 3:14 pm
Location: Sheffield, UK
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby AlexCuse on Mon Jan 26, 2009 6:27 pm

quick and dirty F#, painfully slow

Code is hidden, SHOW
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021
 
Posts: 5411
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby shamsm on Tue Jan 27, 2009 1:47 am

Here is my solution in Java

Code is hidden, SHOW


Code is hidden, SHOW

  1. Total Time    50.9375 ms
  2.  


Edited:
Added initialization code for setting 1 as not prime and 5 and 7 to be divisble by themselves ;)
Time is being printed after sysout of the result string
Last edited by shamsm on Thu Jan 29, 2009 3:22 am, edited 1 time in total.
shamsm
Apprentice
Apprentice
LTD Bronze - Rating: 47
 
Posts: 12
Joined: Wed Jul 30, 2008 10:09 am
Unrated

Re: LTD Puzzle 19: Take numbers between 1 and 100

Postby gmmastros on Tue Jan 27, 2009 2:03 am

There are a couple of minor problems.

1 is not a prime number.
5 is divisible by 5
7 is divisible by 7
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Next