- Calculated in 20.0288 milliseconds
- +------+-----+--------+--------+--------------+
- |Number|Prime|Div By 5|Div By 7|Div By 5 And 7|
- +------+-----+--------+--------+--------------+
- |1 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |2 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |3 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |4 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |5 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |6 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |7 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |8 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |9 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |10 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |11 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |12 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |13 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |14 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |15 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |16 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |17 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |18 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |19 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |20 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |21 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |22 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |23 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |24 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |25 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |26 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |27 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |28 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |29 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |30 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |31 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |32 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |33 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |34 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |35 |no |yes |yes |yes |
- +------+-----+--------+--------+--------------+
- |36 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |37 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |38 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |39 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |40 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |41 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |42 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |43 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |44 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |45 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |46 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |47 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |48 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |49 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |50 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |51 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |52 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |53 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |54 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |55 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |56 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |57 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |58 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |59 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |60 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |61 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |62 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |63 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |64 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |65 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |66 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |67 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |68 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |69 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |70 |no |yes |yes |yes |
- +------+-----+--------+--------+--------------+
- |71 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |72 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |73 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |74 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |75 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |76 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |77 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |78 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |79 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |80 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |81 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |82 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |83 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |84 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |85 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |86 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |87 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |88 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |89 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |90 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |91 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |92 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |93 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |94 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |95 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
- |96 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |97 |yes |no |no |no |
- +------+-----+--------+--------+--------------+
- |98 |no |no |yes |no |
- +------+-----+--------+--------+--------------+
- |99 |no |no |no |no |
- +------+-----+--------+--------+--------------+
- |100 |no |yes |no |no |
- +------+-----+--------+--------+--------------+
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.
Forum Search
Highly Rated Users
Forum Statistics
UsersTotal Post History
- Posts:
- 45704
- Topics:
- 9397
7-Day Post History
- New Posts:
- 342
- New Topics:
- 80
- Active Topics:
- 89
Our newest member
Other
-
FAQ
All times are UTC [ DST ]
Google Ads
LTD Puzzle 19: Take numbers between 1 and 100
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
28 posts • Page 1 of 2 • 1, 2
Please wait...
LTD Puzzle 19: Take numbers between 1 and 100
Take numbers between 1 and 100 and do the following
result would be something like this
you can choose any language you want
- 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
| number | Prime | div by 5 | div by 7 | div by 5 and 7 |
| 1 | no | no | no | no |
| 2 | yes | no | no | no |
| .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. |
| 35 | no | yes | yes | yes |
| .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. |
| 100 | no | yes | no | no |
you can choose any language you want
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11779
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: Take numbers between 1 and 100 and do the following
Here is my solution in Transact SQL
Code is hidden, SHOW
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11779
- 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
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
This produced the following result on my computer.
(which looks much sexier in the console window!
)
Seems I underestimated the forum, which does print the output in a lovely system font.
nice one! =D
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.
Seems I underestimated the forum, which does print the output in a lovely system font.
nice one! =D
Code is hidden, SHOW
-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
Rizzo,
Check your results again. 5 is divisible 5, right?
Check your results again. 5 is divisible 5, right?
-George
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 19: Take numbers between 1 and 100
-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
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.
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.
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
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 19: Take numbers between 1 and 100
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.
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.
-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
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.
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
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 19: Take numbers between 1 and 100
Constructive criticism is always welcome 

-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
Ok.
I'll put my comments in the hide box so it won't affect the code that others may write.
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
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 19: Take numbers between 1 and 100
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 ^_^
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 ^_^
-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
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
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 19: Take numbers between 1 and 100
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:
And the new output is:
Thanks for the advice gmmastros, you're a legend.
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.
-

rizzo - Apprentice

-

- Posts: 22
- Joined: Wed Jan 21, 2009 11:10 pm
- Location: Devon. UK
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 19: Take numbers between 1 and 100
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.
-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 19: Take numbers between 1 and 100
Chrissie,
In that case a WPF app should be the fastest
In that case a WPF app should be the fastest

Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11779
- 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
-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 19: Take numbers between 1 and 100
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.
edited by chrissie1: used hidecode tags
Cheers for the edit Chrissie
Code is hidden, SHOW
edited by chrissie1: used hidecode tags
Cheers for the edit Chrissie

Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
Grr, and my C# implementation of it was 3ms slower than Chrissie's


Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
That's why I wrote that song for Tina Turner.
-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 19: Take numbers between 1 and 100
I was going for it being because I've only been writing C# for a week and a half

Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: LTD Puzzle 19: Take numbers between 1 and 100
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 19: Take numbers between 1 and 100
Here is my solution in Java
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
Code is hidden, SHOW
Code is hidden, SHOW
- Total Time 50.9375 ms
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

-
- Posts: 12
- Joined: Wed Jul 30, 2008 10:09 am
Re: LTD Puzzle 19: Take numbers between 1 and 100
There are a couple of minor problems.
1 is not a prime number.
5 is divisible by 5
7 is divisible by 7
1 is not a prime number.
5 is divisible by 5
7 is divisible by 7
-George
-

gmmastros - LTD Admin

-










- Posts: 1184
- Joined: Tue Oct 09, 2007 5:19 pm
28 posts • Page 1 of 2 • 1, 2


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